How to get the total number of weeks in the current year?

前端 未结 7 1829
野的像风
野的像风 2021-01-17 09:24

I got below code on stackoverflow which return total number of week in current year, but it is hardcoded which\'ll not work on 2014 and 2016. How I get total number of week

7条回答
  •  无人共我
    2021-01-17 09:54

    the fastest way is: Very important to set properly the Calendar depending from where are you from, because of numbering of weeks is determined to given standards. For example in Europ is ISO 8061. It means that 1 week of the year beginn when 01.01.RRRR is Thursday or further, and week beginns from Monday.

    GregorianCalendar gregorianCalendar = new GregorianCalendar(year,12,31);
    gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY); // week beginn from Monday
    gregorianCalendar.setMinimalDaysInFirstWeek(4); // 1week mininum from Thursday
    int totalWeeks = gregorianCalendar.getMaximum(Calendar.WEEK_OF_YEAR);
    

提交回复
热议问题