Definition of the day of the week in GregorianCalendar

I have the following code that is responsible for creating a GregorianCalendar for handling a date.

TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo");  
TimeZone.setDefault(tz);  
Calendar calendar = GregorianCalendar.getInstance(tz);
calendar.setTime(new Date());

System.out.println(calendar.getFirstDayOfWeek());

But what is displayed is "2", that is, Monday. What is the reason for this?

Author: Maniero, 2013-12-11

3 answers

Up To Java 7

For the purpose of the following discussion, note that France starts the week on Monday while the US starts on Sunday.

The code below is running in Scala'S REPL, calling the Java libraries with the following import:

scala> import java.util.{Calendar, Locale, TimeZone}
import java.util.{Calendar, Locale, TimeZone}

TimeZone does not contain the correct information about the first day of the Week

As can be seen:

scala> "startDayOfWeek[^,]*".r findFirstIn TimeZone.getTimeZone("Europe/Paris").toString
res0: Option[String] = Some(startDayOfWeek=1)

scala> "startDayOfWeek[^,]*".r findFirstIn TimeZone.getTimeZone("America/Sao_Paulo").toString
res1: Option[String] = Some(startDayOfWeek=1)

scala> "startDayOfWeek[^,]*".r findFirstIn TimeZone.getTimeZone("America/Los_Angeles").toString
res2: Option[String] = Some(startDayOfWeek=1)

There is no way to extract day of the week from a TimeZone

Using tab-completion to get the available methods:

scala> TimeZone.getTimeZone("Europe/Paris")
res3: java.util.TimeZone = sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transit
ions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMod
e=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endT
ime=3600000,endTimeMode=2]]

scala> res3.
asInstanceOf           clone                  getDSTSavings          getDisplayName         getID
getOffset              getRawOffset           hasSameRules           inDaylightTime         isInstanceOf
observesDaylightTime   setID                  setRawOffset           toString               useDaylightTime

None of this returns the desired information (which is incorrect anyway)

Locale can be used to set the first day of the Week

As we can observe:

scala> Calendar.getInstance(Locale.FRANCE).getFirstDayOfWeek
res8: Int = 2

scala> Calendar.getInstance(Locale.US).getFirstDayOfWeek
res9: Int = 1

But that doesn't work for us Brazilians

scala> Calendar.getInstance(new Locale("pt", "BR")).getFirstDayOfWeek
res10: Int = 2

Ask any Brazilian what is the first day of the week, and he will probably say Sunday. So much so that Monday is the first day useful . Unfortunately, there is no Brazilian standard on this, and the international standard (as well as the custom in most of the world, excluding the United States) dictates Monday as the first day.

In the abnt standard that would be the most relevant, NBR 5892: 1989, there is no statement of which is the first day of the week, but the days of the week are listed from Monday to Sunday when the standard lists the abbreviations.

Na wikipedia, the discussion goes far!

Also, Sunday is part of the "weekend," isn't it?

So, and although we Brazilians use calendars starting with Sunday, I find it difficult to change that. But for those who are sufficiently indignant and motivated, a help:

  • bug submission link ;
  • a previous bug reporting this kind of problem -- in case the bug was invalid, but you can use it as a reference, and copy the program used in it to demonstrate the problem.

Java 8

Java 8's new time and date facilities exhibit the same behavior as Java 7:

import java.time.DayOfWeek;
import java.time.temporal.WeekFields;
import java.util.Locale;

// ...
  WeekFields brWeekFields = WeekFields.of(new Locale("pt", "BR"));
  DayOfWeek brFirstDayOfWeek = brWeekFields.getFirstDayOfWeek(); // MONDAY

But if you want weeks starting on Sunday, use the following:

  WeekFields brWeekFields = WeekFields.SUNDAY_START;
  DayOfWeek brFirstDayOfWeek = brWeekFields.getFirstDayOfWeek(); // SUNDAY

Similarly to Java 7, dates obtained from timezone (zoneId in Java 8) have no information regarding the first day of the week. Unlike Java 7, however, neither there are methods that allow you to make this inquiry into anything derived from zoneId.

 11
Author: Daniel C. Sobral, 2014-01-11 01:47:56

The first day of the week, returned by the function getFirstDayOfWeek() it is defined by the settings of the current locale . For example, in Brazil the convention is that the first day of the week is Monday, while in the US the week begins on Sunday:

Calendar IBrazil Calendar

Edit: if you want to move the current calendar date to the first day of the week it is on:

calendar.setWeekDate(calendar.get(Calendar.YEAR),
                     calendar.get(Calendar.WEEK_OF_YEAR),
                     calendar.getFirstDayOfWeek())

Or simply:

calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
 10
Author: Marco Aurélio, 2014-01-11 01:45:47

O TimeZone It has nothing to do with the week definition. This is controlled by Locale. So much so that if you do this:

for (String zone : TimeZone.getAvailableIDs()) {
    TimeZone tz = TimeZone.getTimeZone(zone);
    TimeZone.setDefault(tz);
    Calendar calendar = GregorianCalendar.getInstance(tz);
    System.out.println(zone + "=" + calendar.getFirstDayOfWeek());
}

The result of getFirstDayOfWeek() It will be the same for all timezones. Already if you do this:

TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo");
TimeZone.setDefault(tz);
for (Locale loc : Locale.getAvailableLocales()) {
    Locale.setDefault(loc);
    Calendar calendar = GregorianCalendar.getInstance(tz);
    System.out.println(loc + "=" + calendar.getFirstDayOfWeek());
}

The result of getFirstDayOfWeek() can be 1, 2 or 7, depending on the Locale used. Incidentally, the TimeZone makes no difference in this code, I could simply call getInstance() (or still getInstance(loc) instead of using Locale.setDefault), that the result it would be the same: the locale defines what will be the first day of the week, regardless of the timezone used.

I also removed the line calendar.setTime(new Date()), as it is redundant: getInstance already takes the current date/time, and setting a new Date() (which is also the current date/time) is unnecessary.


When you create a Calendar, It will fetch within the JVM itself the information about the first day of the week, based on the locale informed. If none is passed to getInstance, we use the default of the JVM (which we changed with Locale.setDefault in the code above).

Basically, the JVM has multiple properties files containing the respective information of each locale. But not all locales have a specific file, and for these there is a default which is used as fallback.

In Java 7, for example, the file default (used as fallback) is that , and see that it has the following content:

firstDayOfWeek=1
minimalDaysInFirstWeek=1

And since there is no specific file for locale pt_BR, the code below-tested in JDK 1.7.0_80-prints 1 (which corresponds to Sunday):

System.out.println(Calendar.getInstance(new Locale("pt", "BR")).getFirstDayOfWeek());

I just found it curious that in another answer the result was 2, because testing in Java 7 and 8, I got 1.

Already using another Local :

System.out.println(Calendar.getInstance(new Locale("en", "GB")).getFirstDayOfWeek());

In this case the result was 2 (Monday ), because in Java 7 there is a file of properties specific to the locale en_GB, in which the value of firstDayOfWeek is 2.

Note: in the links above (which correspond to OpenJDK code) there is no properties for pt_BR, but in the JVM I am using on my machine there is. In any case, what matters is that this information is always sought in the JVM.


In Java 8 is similar: there is a properties {[49] } specific to pt_BR (in which firstDayOfWeek is 1) and there is also other for en_GB (with firstDayOfWeek equal to 2), plus a default properties used as fallback (with firstDayOfWeek equal to 1).

Incidentally, testing in Java 8 (JDK 1.8.0_202) I also got a different result from another answer :

WeekFields brWeekFields = WeekFields.of(new Locale("pt", "BR"));
DayOfWeek brFirstDayOfWeek = brWeekFields.getFirstDayOfWeek(); // SUNDAY

No IdeOne.com the code above also returns "Sunday".


Starting with Java 8, JDK began to seek greater compatibility with the CLDR - Unicode common Locale Data Repository, a repository of information related to locales. One of the information defined by the CLDR is - guess - the definition of the first day of the week . These settings are in the file supplementalData:

<firstDay day="mon" territories=" 001 AD AI AL AM AN AT AX AZ BA BE BG BM BN BY CH CL CM CR CY CZ DE DK EC EE ES FI FJ FO FR GB GE GF GP GR HR HU IE IS IT KG KZ LB LI LK LT LU LV MC MD ME MK MN MQ MY NL NO NZ PL RE RO RS RU SE SI SK SM TJ TM TR UA UY UZ VA VN XK"/>    
<firstDay day="fri" territories="MV"/>    
<firstDay day="sat" territories="AE AF BH DJ DZ EG IQ IR JO KW LY OM QA SD SY"/>    
<firstDay day="sun" territories=" AG AR AS AU BD BR BS BT BW BZ CA CN CO DM DO ET GT GU HK HN ID IL IN JM JP KE KH KR LA MH MM MO MT MX MZ NI NP PA PE PH PK PR PT PY SA SG SV TH TT TW UM US VE VI WS YE ZA ZW"/>

I didn't test for everyone, but see that for BR (Brazil) the first day of the week is Sunday ("sun") and for GB (UK), it is Monday ("mon"), which beat with Java results. In the case of Java 8, both Calendar and WeekFields they use the same properties and the results match the suplementalData above.

But it is worth remembering that since this information is embedded in the JVM, it can change from one version to another, as happened with some locales in Java 9 - after all, CLDR is always being updated, and Java tries to keep up with it as much as possible (besides, CLDR only became the default source of information from the locales from Java 9 ).

The only mystery, to me, is how they got into these values. Someone had to decide which is the first day of the week for each of these locales. That is, this answer helps explain where this data comes from, but not because the data is exactly that.

 1
Author: hkotsubo, 2020-04-30 11:16:59