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,616
  • 1
  • 7
  • 21
Evgeny
  • 2,041
  • 19
  • 29

1 Answers1

6

Try setting Monday as first day of the week.

cal.setFirstDayOfWeek(Calendar.MONDAY);
Kostas Kryptos
  • 3,959
  • 2
  • 22
  • 23