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?
Try setting Monday as first day of the week.
cal.setFirstDayOfWeek(Calendar.MONDAY);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With