I'm trying to generate some code for our financial department, and they require a field that will give them the current week of the year, but based off of Sunday being the first day of the week. So for instance 01-25-16 (MM-dd-yyyy) would be week 4. However when I try it using java.util.Calendar
and calendar.setFirstDayOfWeek
to Sunday, it tells me that this is week 5 because it seems to be counting from the end of December to the 3rd of January as week 1.
Here is the code I've written so far:
private static int getWeekOfYearBySunday(DateTime dt){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dt.getMillis());
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
return calendar.get(Calendar.WEEK_OF_YEAR);
}
This returns a week number of 5, even though it should be 4 if basing the start of the week on Sunday.