Definition of the day of the week in the GregorianCalendar

18

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

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

    
asked by anonymous 11.12.2013 / 21:29

2 answers

12

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 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 the correct information about the first day of the week

As you can see:

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 a TimeZone's day of the week

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 see:

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

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

But that does not work for us Brazilians

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

Ask a Brazilian whatever the first day of the week, and he will probably say Sunday. So much so that Monday is the first useful day. Unfortunately, there is no Brazilian standard on this, and the international standard (just like 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. >

In wikipedia, the discussion goes away!

Also, Sunday is part of the "weekend", is not it?

So, although we Brazilians use calendars starting with Sunday, I find it difficult to change that. But for anyone who is sufficiently outraged and motivated, help:

  • Link to bug submission ;
  • A previous bug reporting this type of problem - 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 problem.

Java 8

New Java 8 time and date facilities have 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

As in Java 7, dates derived from timezone ( zoneId in Java 8) have no information regarding the first day of the week. Unlike Java 7, however, there are no methods to make this query into anything derived from zoneId .

    
11.12.2013 / 23:19
9

The first day of the week, returned by the function getFirstDayOfWeek() is defined by the current locale settings . 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:

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

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

Or simply:

calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
    
11.12.2013 / 21:43