The TimeZone
has nothing to do with the definition of the week. 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()
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()
may be 1, 2 or 7, depending on the Locale
used. Moreover, the TimeZone
makes no difference in this code, I could simply call getInstance()
(or getInstance(loc)
instead of using Locale.setDefault
), that the result would be the same: the locale is what defines the first day of the week, regardless of the Timezone used.
I also removed the line calendar.setTime(new Date())
, because it’s redundant: getInstance
already picks up the current date/time, and set a new Date()
(which is also the current date/time) is unnecessary.
When you create a Calendar
, he will seek inside the JVM the information on the first day of the week, based on the locale informed. If none is passed to getInstance
, use the default of the JVM (which we changed with Locale.setDefault
in the code above).
Basically, the JVM has several files from properties containing the respective information of each locale. But not all the 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 as there is no specific file for the locale pt_BR
, the code below - tested in JDK 1.7.0_80 - print 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 locale:
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 above links (which correspond to the Openjdk code) there is no properties for pt_BR
, but in the JVM I am using on my machine exists. Anyway, what matters is that this information is always searched in the JVM.
In Java 8 it is similar: there is a properties specific to pt_BR
(in which firstDayOfWeek
is 1) and there is also another to en_GB
(with firstDayOfWeek
equal to 2), in addition to a properties default used as fallback (with firstDayOfWeek
equal to 1).
By the way, testing in Java 8 (JDK 1.8.0_202) also got a different result than another answer:
WeekFields brWeekFields = WeekFields.of(new Locale("pt", "BR"));
DayOfWeek brFirstDayOfWeek = brWeekFields.getFirstDayOfWeek(); // SUNDAY
In the Ideone.com the above code also returns "Sunday".
Starting from Java 8, the JDK started to seek a greater compatibility with the CLDR - Unicode Common Locale Data Repository, an information repository related to locales. One of the information defined by CLDR is - guess what - the definition of the first day of the week. These settings are stored in the 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 haven’t tested for everyone, but see that for BR
(Brazil) the first day of the week is Sunday ("sun") and for GB
(United Kingdom), is Monday ("mon"), which match the results of Java. In the case of Java 8, both Calendar
how much WeekFields
use the same properties and the results match with the suplementalData
above.
But it is worth remembering that, as this information is embedded in the JVM, they can change from one version to another, as with some locales in Java 9 - After all, CLDR is always being updated, and Java tries to follow it as far as possible (besides, CLDR has only become the source default of information from locales starting from Java 9).
The only mystery, for me, is how they arrived at 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 are exactly this.
Detail,
getInstance
is a method defined inCalendar
, and not inGregorianCalendar
. Java allows calling static methods through "inheritance", but in practice you are calling the methodCalendar
.– Daniel C. Sobral