Either I don't understand the method getActualMaximum(int)
or the field WEEK_OF_YEAR, or there's a Sun bug involved (or all three)...could someone explain to me why (at least in a German locale...) the following code:
Locale.setDefault( Locale.GERMAN );
Calendar c = Calendar.getInstance();
c.set( Calendar.YEAR, 2010 );
c.set( Calendar.MONTH, 0 );
c.set( Calendar.DAY_OF_MONTH, 1 );
System.out.println("max: "+c.getActualMaximum( Calendar.WEEK_OF_YEAR ));
System.out.println("actual: "+c.get( Calendar.WEEK_OF_YEAR ));
produces the following output:
max: 52
actual: 53
Here's the Javadoc of getActualMaximum(int)
:
Returns the maximum value that the specified calendar field could have, given the time value of this Calendar. For example, the actual maximum value of the MONTH field is 12 in some years, and 13 in other years in the Hebrew calendar system.
Edit
The plot thickens. In an English locale (-Duser.language=en -Duser.country=us
) the output is:
max: 52
actual: 1
Seems to point to it being a Sun bug for German locales?
This information is correct:
max: 52
actual: 53
The year 2010 has a maximum of 52 weeks. The actual week is 53, since 2009 has maximum 53 weeks and most weeks start on sunday or monday. Week 1 is in most cases the first week of the year with 4 days in january. Since the week of the january 1st has only 2 or 3 days in 2010, the week is considered part of 2009.
Most likely the english locale has different rules for determing week 1, like the first week is the week of january 1st.
Wikipedia explains it correctly: wikipedia week article
The problem is, that January 1st 2010 is in week 53 of 2009 (in Germany), but year 2010 only has 52 weeks (December 31st 2010 is in week 52). The Java Calendar object unfortunately does not have a field for the year, to which the week number relates.
来源:https://stackoverflow.com/questions/2047673/calendar-getactualmaximumcalendar-week-of-year-weirdness