更新 在此处放置工作/固定代码只是为了完整起见。 可以在 here 找到有关原因的详细说明。 感谢用户 Perception 指示我。
以下是代码:
import java.util.Locale;
/**
* Demonstrating proper usage of Java's {@linkplain Locale} class
* in respect to accessing user's regional settings, specifically
* the locale configured for date/time/number formatting.
*
* @see Locale#getDefault(java.util.Locale.Category)
* @see Locale#getDefault()
*/
public class DefaultLocaleChangesJava7Demo {
private static final String NO_VALUE = "<- empty ->";
private static final String[] ENVVAR_NAMES = {
"java.runtime.name", "java.runtime.version", "java.vm.name", "java.vm.version",
"os.name", "os.version", "os.arch",
"user.language", "user.country", "user.script", "user.variant",
"user.language.format", "user.country.format", "user.script.format" };
public static void main(String[] args) {
// Print out select environment variables.
System.out.println();
System.out.println("System Information (environment):");
for (String name : ENVVAR_NAMES) {
String value = System.getProperty(name);
if (value == null || value.length() == 0) {
value = NO_VALUE;
}
System.out.println(" " + name + " = " + value);
}
// Print out default display locale information.
System.out.println("\nDefault Display Locale:");
final Locale defaultLocale = Locale.getDefault();
System.out.println(localeToString(defaultLocale, " default."));
// Print out default format locale information.
System.out.println("\nDefault Format Locale:");
final Locale formatLocale = Locale.getDefault(Locale.Category.FORMAT);
System.out.println(localeToString(formatLocale, " format."));
}
public static String localeToString(Locale locale, String prefix) {
final StringBuffer result = new StringBuffer();
result.append(prefix);
result.append("locale.language = ");
result.append(locale.getLanguage());
result.append(" (");
result.append(locale.getDisplayLanguage());
result.append(")\n");
result.append(prefix);
result.append("locale.country = ");
result.append(locale.getCountry());
result.append(" (");
result.append(locale.getDisplayCountry());
result.append(")\n");
result.append(prefix);
result.append("locale.script = ");
result.append(locale.getScript());
result.append(" (");
result.append(locale.getDisplayScript());
result.append(")\n");
result.append(prefix);
result.append("locale.variant = ");
result.append(locale.getVariant());
result.append(" (");
result.append(locale.getDisplayVariant());
result.append(")\n");
return result.toString();
}
}
今天我注意到Java Locale.getDefault()
没有返回我在Windows 7帐户中配置的区域设置。
演示应用程序:
import java.util.Locale;
public class BadLocaleDemo {
private static final String NO_VALUE = "<- empty ->";
private static final String[] ENVVAR_NAMES = {
"java.runtime.name", "java.runtime.version", "java.vm.name", "java.vm.version",
"os.name", "os.version", "os.arch" };
public static void main(String[] args) {
// Print out select environment variables.
System.out.println();
System.out.println("System Information (environment):");
for (String name : ENVVAR_NAMES) {
String value = System.getProperty(name);
if (value == null || value.length() == 0) {
value = NO_VALUE;
}
System.out.println(" " + name + " = " + value);
}
// Print out default locale information.
System.out.println();
System.out.println("Default Locale:");
final Locale defaultLocale = Locale.getDefault();
System.out.println(" default.locale.language = " + defaultLocale.getLanguage());
System.out.println(" default.locale.country = " + defaultLocale.getCountry());
System.out.println(" default.locale.display.language = " + defaultLocale.getDisplayLanguage());
System.out.println(" default.locale.display.country = " + defaultLocale.getDisplayCountry());
}
}
并打印出来的信息:
System Information (environment):
java.runtime.name = Java(TM) SE Runtime Environment
java.runtime.version = 1.7.0_02-b13
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
java.vm.version = 22.0-b10
os.name = Windows 7
os.version = 6.1
os.arch = amd64
Default Locale:
default.locale.language = en
default.locale.country = US
default.locale.display.language = English
default.locale.display.country = United States
默认本地应该是sr_RS
根据我的区域设置(工作日的名称在托盘时钟上是cyrilic),但Java正在报告en_US
。
有没有人知道如何解决这个问题?
我在StackOverflow上找到的相关帖子只有这两个: