java – Weekday setting in GregorianCalendar

Question:

I have the following code which 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 happening?

Answer:

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 being executed in the Scala 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 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 weekdays from a TimeZone

Using tab-completion to get 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 the first day of the week is, and he will probably say Sunday. So much so that Monday is the first working day. Unfortunately, there is no Brazilian standard on this, and the international standard (as well as the custom in most parts of the world, excluding the United States) dictates Monday as the first day.

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

On wikipedia, the discussion goes far!

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

Therefore, 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:

  • Link to submit bugs ;
  • An earlier bug reporting this type of issue — in this case the bug was invalid, but you can use it as a reference, and copy the program used in it to demonstrate the issue.

Java 8

The new time and date facilities in Java 8 have the same behavior as in 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

As in Java 7, dates obtained from timezone ( zoneId in Java 8) do not have information about the first day of the week. Unlike in Java 7, however, there aren't even methods that allow you to do this query on anything derived from zoneId .

Scroll to Top