The ISO-8601 standard states that
"The first week of a year is the week that contains the first Thursday of the year (and, hence, always contains 4 January)."
Meaning the first week of the year is not that which contains January the 1st but the first one that contains at leat four days into the new year.
Acording to that Mon, January 11 2016 is on week #2. Here is a list of week numbers for 2016.
Ubuntu reflects that in its time widget:
And the cal command does also:
Oracle supports it with the "iw" parameter of TO_CHAR
:
> select to_char(to_date('11/01/2016','dd/mm/yyyy'),'iw') weekno from dual;
> WEEKNO
02
But Java says Mon, January 11 2016 is week #3
Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
System.out.println(c.get(Calendar.WEEK_OF_YEAR));
Output:
Mon Jan 11 09:02:35 VET 2016
3
Java thinks the first week of the year is the one that contains January the 1st.
- Is there a way for Java to use the ISO-8601-copliant week numbering?
As I noted in my comment, the default behavior is locale specific. Some locales will give 3, some will give 2.
Luckily, you can specify the number of days that has to be present in the first week of the year, for a given Calendar
. As you write above, for ISO 8601, this number is 4
, thus the following code should work:
Calendar c = Calendar.getInstance();
c.setMinimalDaysInFirstWeek(4); // For ISO 8601
System.out.println(c.getTime());
System.out.println(c.get(Calendar.WEEK_OF_YEAR));
This should make the output correct regardless of locale.
Test output:
Mon Jan 11 14:54:22 CET 2016
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With