8

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?

Wyatt Shipman
  • 1,669
  • 1
  • 10
  • 22
Evgeny
  • 2,121
  • 1
  • 20
  • 31

1 Answers1

6

Try setting Monday as first day of the week.

cal.setFirstDayOfWeek(Calendar.MONDAY);
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24