-1

By the following way I set date object to calendar instance. enter image description here

But when I get the week of year , wrongly it returns week of the year as 1. Same as week of the year, week of the month value also wrong. It return as 6. But it should be 5.

Following is output of calendar instance value

java.util.GregorianCalendar[time=1514678400000, 
areFieldsSet=true, 
areAllFieldsSet=true, 
lenient=true, 
zone=sun.util.calendar.ZoneInfo[id="UTC", 
offset=0, dstSavings=0, useDaylight=false, transitions=0, lastRule=null], 
firstDayOfWeek=1, minimalDaysInFirstWeek=1, ERA=1, YEAR=2017, MONTH=11, WEEK_OF_YEAR=1, WEEK_OF_MONTH=6, DAY_OF_MONTH=31, DAY_OF_YEAR=365, DAY_OF_WEEK=1, DAY_OF_WEEK_IN_MONTH=5, AM_PM=0, HOUR=0, HOUR_OF_DAY=0, MINUTE=0, SECOND=0, MILLISECOND=0, ZONE_OFFSET=0, DST_OFFSET=0]
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
gangatharan
  • 781
  • 1
  • 12
  • 28

1 Answers1

5

firstDayOfWeek=1 means that Sunday is the first day of the week. So Sunday December 31, 2017 belonged to week 1 of 2018.

It’s not well documented, but it turns out that the week of month follows the month, so on Dec 31, it gives you the number of the week in December. Since December 1 and 2 were Friday and Saturday, they formed week 1 of December. Week 2 began on December 3. So week 6 began on December 31 and lasted only that day. This explains why week of month is 6 in your case.

Whether we use IST time zone (I am assuming it means Asia/Kolkata) or UTC doesn’t make a difference. At 5:30 AM in India, it was 00:00 in UTC, the date was still December 31, and therefore the week numbers are also unchanged.

Your Calendar’s settings of first day of week and minimal days in first week follow the standard used in USA and some other countries. If instead you wanted the international standard where Monday is the first day of the week and there is a minimum of 4 days in week one, you may instantiate it with a locale of a country that uses the international standard, for example:

    Calendar c = Calendar.getInstance(Locale.FRANCE);

In this case week of year will be 52 and week of month will be 4. Not 5. This is because the first three days of December are not enough to form a week, so week 1 of December begins on December 4. Then the first three days of December are in week 0.

Prefer to use java.time

All of that said, the Calendar class is long outmoded and poorly designed. I recommend you use java.time, the modern Java date and time API, instead. Specifically, the LocalDate & WeekFields classes.

For example:

    LocalDate date = LocalDate.of(2017, Month.DECEMBER, 31);
    System.out.println("ISO week of year  " + date.get(WeekFields.ISO.weekOfWeekBasedYear()));
    System.out.println("ISO week of month " + date.get(WeekFields.ISO.weekOfMonth()));
    System.out.println("US week of year   " + date.get(WeekFields.SUNDAY_START.weekOfWeekBasedYear()));
    System.out.println("US week of month  " + date.get(WeekFields.SUNDAY_START.weekOfMonth()));

Output:

ISO week of year  52
ISO week of month 4
US week of year   1
US week of month  6

The results agree with those we got from Calendar.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161