different locale pattern on standalone Java application and Web application

半城伤御伤魂 提交于 2019-12-24 02:09:34

问题


Why there is a different outputs for below code when tried as a standalone Java application and as a Web application (Servlet)?

Code:

Locale locale=new java.util.Locale("nb");// locale for Norwegian, Bokmal (Norway)
DecimalFormat decimalFormatter=(DecimalFormat) DecimalFormat.getInstance(locale);

System.out.println(decimalFormatter.toLocalizedPattern());
System.out.println(locale.getDisplayLanguage());
System.out.println(decimalFormatter.format(1234.567));

Outputs:

standalone JAVA application:

# ##0,###
Norwegian Bokmål
1 234,567

as a WEB application (Servlet)

#,##0.###
Norwegian Bokmål
1,234.567

Java Version : JDK.1.6.0_23 Web Server : Jetty enclosed in Eclipse Juno


EDIT

Here is the some findings as suggested by @haraldK

Is it possible to get the NumberFormat as provided by standard Java implementation rather than provider implementation?

Here is the source code of NumberFormat.getInstance(Locale) that internally calls private method NumberFormat.getInstance(Locale,int) as shown below:

private static NumberFormat getInstance(Locale desiredLocale,
                                       int choice) {
    // Check whether a provider can provide an implementation that's closer
    // to the requested locale than what the Java runtime itself can provide.
    LocaleServiceProviderPool pool =
        LocaleServiceProviderPool.getPool(NumberFormatProvider.class);
    if (pool.hasProviders()) {
        NumberFormat providersInstance = pool.getLocalizedObject(
                                NumberFormatGetter.INSTANCE,
                                desiredLocale,
                                choice);
        if (providersInstance != null) {
            return providersInstance;
        }
    }
    ...
}

来源:https://stackoverflow.com/questions/23613071/different-locale-pattern-on-standalone-java-application-and-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!