2

I am actually going through the utility class Calender. I find myself confused with,

The methods

getFirstDayOfWeek() 
returns the first day of the week;
e.g., SUNDAY in the U.S., MONDAY in France.

and

getMinimalDaysInFirstWeek()
returns the minimal days required in the first week of the year. e.g., 
if the first week is defined as one that contains the first day of the
first month of a year, this method returns 1. If the minimal days required
must be a full week, this method returns 7.

I am living in India (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi. Daylight Saving time is not observed in this time zone.

To get the Week of the year, i coded like this

Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));   // returns 45 

45 is only possible if Java treats

WeekNumber   FromDate                       ToDate
Week 01      December 30, 2013              January 5, 2014  // December 30 is Monday
Week 45      November 3, 2014(Monday)       November 9, 2014

Does Java takes minimal days to be a full week to be 7? Because I can change it through setMinimalDaysInFirstWeek(). And what effect will it have?

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105

1 Answers1

3

It's relevant for the number of days in a week of the week-year. Unless you're interested in week-years, it's probably irrelevant to you. By default I'd expect it to be 4, as that's the normal ISO-8601 value.

I don't know for sure whether that interacts with the "first day of week" part - you'd need to check, but it sounds like it does. It shouldn't have anything to do with your time zone.

But for example, week-year 2014 starts on December 30th 2013 precisely because that gives 5 days in 2014. If you look at some other years, you'll find ones where the first couple of days of January calendar year X are in week 52 or week 53 of week-year X-1.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It's indeed 4. Docs illustrate it nicely. For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997. – Farhan stands with Palestine Nov 03 '14 at 18:14