这两个问题让我想知道 Mac OS X 中的哪些设置会影响 区域设置 和 日历 Java 中的默认值和行为:
These two questions prompted me to wonder what settings in Mac OS X affect the Locale and Calendar defaults and behavior in Java:
这些讨论的关键是日历中的这两个属性:
Key in those discussions are these two properties in Calendar:
这两个值的默认值是 Java 7 中的 1
&8 在默认美国上运行时.什么会导致报告其他值?
The default for both those values is 1
in Java 7 & 8 when run on a default United States. What can cause other values to be reported?
我看到了一些奇怪的行为,这些行为会影响 java.util.Calendar 的这些属性.
I've seen some peculiar behavior as to what affects these properties of java.util.Calendar.
确定的事实:
Locale
由 System Preferences 中的 Language 确定.Locale
is determined by Language in System Preferences.将此代码作为测试运行.
Running this code as a test.
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class TestCalendar
{
public static void main( String[] args )
{
Locale locale = Locale.getDefault();
Calendar c = Calendar.getInstance();
c.setTime( new Date( new Long( 1293840000000l ) ) ); // First moment of the year 2011 in UTC.
System.out.println( "Locale: " + locale + " | FirstDayOfWeek: " + c.getFirstDayOfWeek() + " | MinimialDaysInFirstWeek: " + c.getMinimalDaysInFirstWeek() );
}
}
在 Mac OS X (Mavericks) 上托管的 Parallels 9 虚拟机中使用 Mac OS X 10.8.5 (Mountain Lion),带有 Java 8 Update 11,在安装操作系统,我玩弄 System Preferences
> Language &文本
.
Using Mac OS X 10.8.5 (Mountain Lion) in a Parallels 9 virtual machine hosted on Mac OS X (Mavericks) with Java 8 Update 11 with a United States locale chosen during installation of the OS, I played around with System Preferences
> Language & Text
.
奇怪的是,在 Region 选项卡上更改 First day of week
没有任何效果.Java 报告 FirstDayOfWeek: 1
我是否将该弹出菜单设置为星期日"或星期一".
Strangely, changing First day of week
on the Region tab has no effect. Java reports FirstDayOfWeek: 1
whether I set that popup menu to "Sunday" or "Monday".
Locale: en_US | FirstDayOfWeek: 1 | MinimialDaysInFirstWeek: 1
重新启动 NetBeans IDE 没有帮助.重新启动 Mac(虚拟机)没有帮助.
Restarting the NetBeans IDE does not help. Restarting the Mac (virtual machine) does not help.
在 区域 选项卡上,选中 显示所有区域 复选框以查看更多区域.选择 法语
> France
.立即运行 IDE.不需要重新启动 IDE 或操作系统,甚至不需要关闭 System Preferences 窗口.
On the Region tab, check the Show all regions checkbox to see many more regions. Choose French
> France
. Run the IDE immediately. No need no restart the IDE or the OS, nor even to close the System Preferences window.
Locale: en_US | FirstDayOfWeek: 2 | MinimialDaysInFirstWeek: 4
对两个帐户很感兴趣.
2
表示 Monday
,这对于法国(以及世界大部分地区)是正确的.2
as FirstDayOfWeek means Monday
, as is correct for France (and much of the world).将 Region 弹出窗口重置回 United States
会恢复 Java 属性,这是一致且符合预期的:
Resetting the Region popup back to United States
restores the Java properties, which is consistent and expected:
Locale: en_US | FirstDayOfWeek: 1 | MinimialDaysInFirstWeek: 1
在语言选项卡上,将Franais
(法语)拖到列表顶部,使其出现在英语之前.
On the Language tab, drag Franais
(French) to the top of the list, so it appears before English.
立即运行 IDE.
Locale: fr_FR | FirstDayOfWeek: 1 | MinimialDaysInFirstWeek: 1
再次,有趣.现在我们知道 Java Locale 是由 Mac Language 设置决定的.我们知道这对相关日历属性没有影响.
Again, interesting. Now we know the Java Locale is determined by the Mac Language setting. And we know that has no effect on the Calendar properties in question.
所以您认为 Mac 语言 决定了 Java 语言环境,而 Java 语言环境不 影响日历属性?正确,当阅读上面的内容时,但在阅读下一节时错误,我们看到 Java 语言环境设置了另一种方式可以影响日历属性.非常混乱.
So you think Mac Language determines Java Locale, and Java Locale does not affect the Calendar properties? Right, when reading above, but Wrong when reading on to next section where we see that Java Locale set another way can affect the Calendar properties. Very confusing.
发现另一个矛盾.让我们将 Mac 恢复为美国默认设置:(1) Language 列表顶部的英语,(2) 区域设置为 United States.
Another contradiction found. Let's restore the Mac back to US defaults: (1) English at top of Language list, (2) Region set to United States.
更改我们的代码以将区域设置传递给日历的构造函数.
Change our code to pass a Locale to the Calendar's constructor.
Calendar c = Calendar.getInstance( Locale.FRANCE );
这会影响日历属性:
FirstDayOfWeek: 2 | MinimialDaysInFirstWeek: 4
所以,令人困惑的矛盾是:
So, the confusing contradiction is:
这篇关于Mac OS X 中的哪些设置会影响 Java 中的“Locale"和“Calendar"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!