问题
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