Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What settings in Mac OS X affect the `Locale` and `Calendar` inside 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 inconsistent on different machines
  • Why would Calendar.getInstance() fail to use the default locale?

Key in those discussions are these two properties in Calendar:

  • firstDayOfWeek
  • minimalDaysInFirstWeek

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?

like image 922
Basil Bourque Avatar asked Jul 19 '14 21:07

Basil Bourque


People also ask

How does JVM determine default locale?

That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

How do I change my locale on Mac?

To open this pane, choose Apple menu > System Preferences, click Language & Region , then click General. The languages you want shown in macOS and in apps (for example, in menus and messages), and on websites (if available in a preferred language).


1 Answers

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.

Screen shot of window after choosing Apple menu > System Preferences > Language & Text in Mac OS X Mountain Lion

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.
like image 199
Basil Bourque Avatar answered Sep 19 '22 04:09

Basil Bourque