Error in week of year in java calendar?

The following code calculates the work week of a specific date.

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar cal = new GregorianCalendar(); cal.setTime(df.parse("2015-12-27 08:00:00")); System.err.printf("%d.%02d\n", cal.getWeekYear(), cal.get(Calendar.WEEK_OF_YEAR)); 

He is currently printing 2016.01.

As I understand it, the specification of the number of working weeks, 2016.01 is the first week that has 4 days in 2016, but December 27 cannot belong to this week.

Is there a way to do this in Java 7 that will work for any year, assuming weeks start on Monday?

+8
java date calendar
source share
1 answer

Try setting Monday as the first day of the week.

 cal.setFirstDayOfWeek(Calendar.MONDAY); 
+6
source share

All Articles