28

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".

Neo
  • 1,337
  • 4
  • 21
  • 50
  • 2
    There must be some error. Did you try a simple test app? I'd reinstall it. – Michael-O Aug 18 '11 at 13:14
  • I set a breakpoint just after having started the app and the default locale is en_US. For all persons in my company is the same – Neo Aug 18 '11 at 16:20

7 Answers7

30

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

Eugene Evdokimov
  • 2,220
  • 2
  • 28
  • 33
dream_team
  • 511
  • 5
  • 9
  • 6
    According to the bug report, this "Feature" was fixed in Java 8 build 04. So in Java 8, we can expect the correct behavior. – dARKpRINCE Feb 06 '14 at 11:29
  • 2
    @dARKpRINCE [The fix](https://bugs.openjdk.java.net/browse/JDK-7073906) was actually also backported to 7u4: https://bugs.openjdk.java.net/browse/JDK-2217027 – ntoskrnl Feb 13 '14 at 20:02
  • 1
    I'm not sure what the fix is, I've tried Locale.getDefault() on both the latest release of JDK 7 and latest JDK 8 beta and both return a different result than JDK <= 6 – eagerMoose Feb 28 '14 at 19:17
  • Is this still a valid answer? I guess they've reverted the feature with Java 8. – OddDev Dec 20 '16 at 10:03
  • Should't it work correctly with Locale.getDefault(Category.FORMAT)? – marcolopes Dec 28 '20 at 21:30
16

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.

Vadzim
  • 24,954
  • 11
  • 143
  • 151
Jens Borgland
  • 753
  • 4
  • 15
4

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.

merryprankster
  • 3,369
  • 2
  • 24
  • 26
2

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;
}

}

Alex Kartishev
  • 1,836
  • 3
  • 18
  • 25
2

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'

  • 2
    What about Locale.getDefault(Locale.Category.DISPLAY) and Locale.getDefault(Locale.Category.FORMAT) ? Just curious. – Hakanai Jun 20 '14 at 04:11
0

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.

maryoush
  • 173
  • 1
  • 12
0

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

stracktracer
  • 1,862
  • 5
  • 24
  • 37