Neo
Neo

Reputation: 1377

Java 7 default locale

I have just installed jre7 and I'm surprised to see that my default locale is now en_US. With jre6 it was de_CH.

What is different with jre7? Is the default locale no more the one of the Operating System? (btw, I'm using Windows7)

Thx for your answer.

Edit: I have seen the Locale for Category.FORMAT is the "old" one (de_CH). The Locale for Category.DISPLAY takes the language from the language of the OS (in Windows this is done in Control Panel > Region and Language > Keyboard and Languages > Display Language) and the contry from...?

What seems to be different is the property "user.country". With Java6 I get "CH" and with Java7 I get "US".

Upvotes: 28

Views: 29473

Answers (7)

dream_team
dream_team

Reputation: 521

This is as designed. Java 7 has changed the way Locale.getDefault() works. A defect has been entered with Oracle, but they basically said this is As Designed.

To sum up, you must modify the Display Language of the OS. Modifying the Region Format only is no longer sufficient.

Read the bug report here: Locale.getDefault() returns wrong Locale for Java SE 7

Upvotes: 30

Jens Borgland
Jens Borgland

Reputation: 773

The change is described quite well in this blog post and on the compatibility page.

Note that you can revert to the old behavior by setting the sun.locale.formatasdefault system property to true.

Upvotes: 16

Alex Kartishev
Alex Kartishev

Reputation: 1856

What about setting your Locale at the start of the program in the following way, depending on java version:

public class LocaleFormatter {

private static Locale locale;

private LocaleFormatter() {

}

public static Locale setDefaultLocale() {
    if (locale == null) {
        if (!System.getProperty("java.version").startsWith("1.7.")) {
            locale = Locale.getDefault();
        } else {
            try {
                Class localeClass = Class.forName("java.util.Locale");
                Class categoryClass = Class.forName("java.util.Locale$Category");
                Object format = null;
                for (Object constant : categoryClass.getEnumConstants()) {
                    if (constant.toString().equals("FORMAT")) {
                        format = constant;
                    }
                }
                Method method = localeClass.getMethod("getDefault", categoryClass);

                locale = (Locale) method.invoke(Locale.getDefault(), format);
                Locale.setDefault(locale);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    return locale;
}

}

Upvotes: 2

maryoush
maryoush

Reputation: 173

If you are brave enough you can call the :

Locale.setDefault(Locale.getDefault());

This sets the default Locale for both of those categories

public static synchronized void setDefault(Locale newLocale) {
   setDefault(Category.DISPLAY, newLocale);
   setDefault(Category.FORMAT, newLocale);
   defaultLocale = newLocale;
}

But this of course could cause side effects.

Upvotes: 0

Johan Compagner
Johan Compagner

Reputation: 21

This really looks like a bug to me:

public static void main(String[] args) throws FileNotFoundException, IOException {

System.err.println(Locale.getDefault());
}

running this with java 5 or java 6 prints: 'nl_NL' java7: 'en_US'

Upvotes: 2

stracktracer
stracktracer

Reputation: 1900

Check the setting "Location" in Windows control panel Regional and language options (German: "Region und Sprache", "Aufenthaltsort").

Upvotes: 0

merryprankster
merryprankster

Reputation: 3419

There seems to be some changes regarding Locale in Java 7, namely differentiation between UI and 'user' locale. See this. There is now setDefault(Locale.Category, Locale). However, this does not really explain what you are experiencing - I'm merely pointing out the fact that there has been changes in Java 7 regarding locale handling.

Upvotes: 4

Related Questions