I am using calendar.getActualMaximum(Calendar.WEEK_OF_YEAR) to get total number of weeks in a given year. This is the function I am using :
public static void main(String[] args) {
for(int i=2012; i<=2016; i++) {
System.out.println("Year : " + i + ", Total Weeks : " + getNumWeeksForYear(i));
}
}
public static int getNumWeeksForYear(int year) {
Calendar c = Calendar.getInstance();
c.set(year, 12, 31); //issue
c.setFirstDayOfWeek(Calendar.MONDAY );
return c.getActualMaximum(Calendar.WEEK_OF_YEAR);
}
Output with c.set(year, 12, 31): Year : 2012, Total Weeks : 52 Year : 2013, Total Weeks : 52 Year : 2014, Total Weeks : 52 Year : 2015, Total Weeks : 52 Year : 2016, Total Weeks : 53
Output with c.set(year, 1, 31): Year : 2012, Total Weeks : 53 Year : 2013, Total Weeks : 52 Year : 2014, Total Weeks : 52 Year : 2015, Total Weeks : 52 Year : 2016, Total Weeks : 52
I am unable to figure it out why the total weeks in a year are different when the calendar month is set to Dec. I have tried using months 1-12, for 1-11 the number of weeks are same but changes for the month 12. From the experiment I can predict it only happens with leap years.
I had referred the following link: Calendar.getActualMaximum(Calendar.WEEK_OF_YEAR) weirdness
But this does not answer my query.