在调试我的应用程序时,如果发现DecimalFormat
和DateFormat
默认设置为使用nl_NL
,但系统区域设置似乎是en_US
。
我通过评估找到了第一个:
new DecimalFormat().symbols.locale
new SimpleDateFormat().locale
第二个:
Locale.getDefault()
System.getProperty("user.country")
System.getProperty("user.language")
那里发生了什么?
如何将这些格式化程序设置为默认使用特定区域设置? (我正在编写单元测试,并且希望设置特定的区域设置而不触及应用程序代码)
最佳答案
从 Java 7 开始,the default locale comes in two categories :“显示”和“格式”。
两者都是根据环境初始设置的。可以通过调用 Locale.getDefault(Category)
找到它们。
除了问题中提到的系统属性之外:
"user.country"
"user.language"
运行 JVM 时也可能出现以下内容:
"user.country.format"
"user.language.format"
"user.country.display"
"user.language.display"
通过调用 Locale.setDefault(Locale)
回答我的上一个问题 the default locale will be set for all categories .
关于java - DecimalFormat 和 DateFormat 默认使用什么区域设置? (为什么它与 Locale.getDefault() 不同?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266334/