Java 7 default locale

后端 未结 7 1639
忘了有多久
忘了有多久 2020-11-28 11:25

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

相关标签:
7条回答
  • 2020-11-28 11:33

    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'

    0 讨论(0)
  • 2020-11-28 11:36

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

    0 讨论(0)
  • 2020-11-28 11:45

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

    }

    0 讨论(0)
  • 2020-11-28 11:45

    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.

    0 讨论(0)
  • 2020-11-28 11:55

    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

    0 讨论(0)
  • 2020-11-28 11:56

    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.

    0 讨论(0)
提交回复
热议问题