What settings in Mac OS X affect the `Locale` and `Calendar` inside Java?

流过昼夜 提交于 2019-11-30 21:47:20

I've seen some peculiar behavior as to what affects these properties of java.util.Calendar.

Conclusions

The facts determined:

  • 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).

Details

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() );
    }
}

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.

System Preferences > Language & Text > Region > First day of week

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

Restarting the NetBeans IDE does not help. Restarting the Mac (virtual machine) does not help.

System Preferences > Language & Text > Region

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

Interesting on two accounts.

  • 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.

Resetting the Region popup back to United States restores the Java properties, which is consistent and expected:

Locale: en_US | FirstDayOfWeek: 1 | MinimialDaysInFirstWeek: 1

System Preferences > Language & Text > Language

On the Language tab, drag Français (French) to the top of the list, so it appears before English.

Immediately run the IDE.

Locale: fr_FR | FirstDayOfWeek: 1 | MinimialDaysInFirstWeek: 1

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.

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.

Pass Java Locale

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 );

This affects the Calendar properties:

FirstDayOfWeek: 2 | MinimialDaysInFirstWeek: 4

So, the confusing contradiction is:

  • 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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!