Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A bug in week of year in Java Calendar? [duplicate]

The following code calculates the workweek 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));

It currently prints 2016.01.

As I understand the work week number specification, 2016.01 is the first week having 4 days in 2016, but there is no way December 27 can belong to such week.

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

like image 553
Evgeny Avatar asked Jan 05 '16 09:01

Evgeny


1 Answers

Try setting Monday as first day of the week.

cal.setFirstDayOfWeek(Calendar.MONDAY);
like image 182
Kostas Chalkias Avatar answered Sep 25 '22 15:09

Kostas Chalkias