Java 7 default locale

后端 未结 7 1682
忘了有多久
忘了有多久 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: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;
    }
    

    }

提交回复
热议问题