Mac OS X 中的哪些设置会影响 Java 中的“Locale"和“Calendar"?

2022-01-11 00:00:00 macos week-number calendar dayofweek java

这两个问题让我想知道 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:

  • WEEK_OF_YEAR 在不同机器上不一致
  • 为什么 Calendar.getInstance() 无法使用默认语言环境?

这些讨论的关键是日历中的这两个属性:

Key in those discussions are these two properties in Calendar:

  • firstDayOfWeek
  • minimalDaysInFirstWeek

这两个值的默认值是 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.

确定的事实:

  • Java Locale 由 System Preferences 中的 Language 确定.
  • Calendar 的两个属性不受通过 Mac 的 Language 更改的 Java Locale 的影响.相反,它们是通过在系统偏好设置中选择一个区域来确定的.
  • 奇怪的是,可能是一个错误,在系统偏好设置中手动选择一周的第一天弹出菜单无法影响 Java 中的等效属性.在选择 Region 时影响 Mac 设置会影响 Java,但手动选择弹出菜单不会.
  • 通过 Mac Language 设置设置 Java 区域设置不会影响日历的属性,但将区域设置传递给日历的构造函数会影响其属性(明显矛盾).
  • The Java Locale is determined by Language in System Preferences.
  • The two properties of Calendar are not affected by the Java Locale changed via the Mac’s Language. Instead they are determined by choosing a Region in System Preferences.
  • Oddly enough, and possibly a bug, manually choosing First day of week popup menu in System Preferences fails to affect the equivalent property in Java. Affecting that Mac setting as part of choosing Region affects Java, yet manually choosing the popup menu does not.
  • Setting the Java Locale via the Mac Language setting does not affect the Calendar's properties, yet passing a Locale to the Calendar's constructor does affect its properties (an apparent contradiction).

将此代码作为测试运行.

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

对两个帐户很感兴趣.

  • 现在我们知道 Region 设置会影响这两个关键日历设置,但 Locale 并没有改变.作为 FirstDayOfWeek 的值 2 表示 Monday,这对于法国(以及世界大部分地区)是正确的.
  • 另一个问题很奇怪,可能是一个错误:当设置为较大区域更改的一部分时,一周的第一天弹出窗口似乎会影响 Java,但手动选择该弹出窗口不会影响 Java 属性有问题.
  • Now we know the Region setting affects both of these key Calendar settings, yet the Locale has not changed. The value 2 as FirstDayOfWeek means Monday, as is correct for France (and much of the world).
  • The other issue is bizarre, perhaps a bug: The First day of week popup seems to affect Java when set as part of a larger Region change but manually selecting that popup does not affect the Java properties in question.

将 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

系统偏好设置 > 语言 &文字 > 语言

在语言选项卡上,将Français(法语)拖到列表顶部,使其出现在英语之前.

System Preferences > Language & Text > Language

On the Language tab, drag Français (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 的语言设置 Java 区域设置不会影响日历的属性.
  • 将 Locale 显式传递给 Calendar 构造函数会影响其属性.
  • Setting the Java Locale via the Mac's Language does not affect the Calendar's properties.
  • Explicitly passing the Locale to the Calendar constructor does affect its properties.

相关文章