dataFaber 5.0

Calendar.getFirstDayOfWeek() gotchas

Seemigly innocent statements in the Calendar.getFirstDayOfWeek() Javadoc:

Calendar defines a locale-specific seven day week using two parameters: the first day of the week and the minimal days in first week (from 1 to 7). These numbers are taken from the locale resource data when a Calendar is constructed. They may also be specified explicitly through the API.

Gets what the first day of the week is; e.g., Sunday in US, Monday in France.

(emphasis mine)

I can’t count the number of times that this has tripped up my colleagues: they typically develop on their XP boxes with the locale set to Italian, and then scratch their head perplexed when a nearby Linux box displays the dates with a 1-day offset.

Lesson of the day: RTFJ (Read The FJavadoc).

1 Comment

  1. Maggie Leber

    So true. From EFnet IRC #java eartler this month:

    -------------------------------
    does anybody knwo why new GregorianCalendar().get(GregorianCalendar.WEEK_OF_YEAR); gives me 43 on my linuxcomputer and 42 on windows
    the sysdate is correct on both
    Timezone set correctly on Windows?
    it works on windows
    it is week 42
    in sweden
    Well, on Linux I get 43 also.
    * MaggieL shrugs.
    Also dependant on which day of the week you think is first.
    Might be locale-dependant.
    someplace week start sunday, other on monday
    All the rules for this stuff are in the API.
    strumpen: It is locale-dependant.
    bsh % print(new GregorianCalendar(Locale.FRANCE).get(GregorianCalendar.WEEK_OF_YEAR));
    42
    bsh % print(new GregorianCalendar(Locale.US).get(GregorianCalendar.WEEK_OF_YEAR));
    43
    ok i solved is with g.setFirstDayOfWeek
    --------------------------------------