12

I have the following code in a JUnit test, which seemed to work last week is failing this week:

Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.JULY, 12);
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); // push the date to 15
System.out.println(cal.get(Calendar.DATE));

As you could probably infer from my comment, since the 12th is a Tuesday, I expect Date to be 15 after setting the DAY_OF_WEEK to Friday. However, the value that is printed is 22, and causes the test to fail.

If I, however change the code as follows, and add an additional call to get:

Calendar cal = Calendar.getInstance();
cal.set(2011, Calendar.JULY, 12);
System.out.println(cal.get(Calendar.DATE));
cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); // push the date to 15
System.out.println(cal.get(Calendar.DATE));

I get the output that I expect, 12 and 15.

Can someone explain what is going on, and why this test was working last week?

Jeshurun
  • 22,940
  • 6
  • 79
  • 92

3 Answers3

19

The first thing to understand is that Month + Day + DayOfWeek does not mean anything to the Calendar. The Calendar will calculate the true value of the date based on

YEAR + MONTH + DATE

or

YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

(Or some other combos like year + day of year etc.) So Date + DayOfWeek doesn't inherently mean much to it.

The second thing to understand is when you set on a Java Calendar it doesn't actually recompute the absolute time or update related fields until an operation that forces computation occurs.

After your first set, the calendar is in a conflicted state. The month and day say that it's July 12th, but the 'week of month' and 'day of week' still say that it's today, whatever today is. You then call set day of week to friday. So now year month and day say July 12th, but the 'week of month' and 'day of week' fields say it's Friday of 'this' week.

The rules of the calendar say that the most recently set field "wins" when there's a conflict, so the week of month and day of week combining to say Friday of this week are what's used to calculate the other fields.

Inserting a get in the middle 'fixes' it because it forces the entire internal state of the calendar to get recomputed to Tuesday July 12th before setting to Friday, so there are no internal conflicts. The 'week of month' got set to the week that contains July 12th by the recalculation prior to you setting day of week to Friday.

Edit: Sorry to make changes after two days, noticed this open in an old browser tab and thought I would expand for the hopeful help of future googlers:

The reason it worked for Jon in the comments is he lives in London. His computer thinks weeks start on Mondays. So when asked for Friday of 'this' week, it still responded July 15th when asked on Sunday July 17th. I bring this up because differing first days of the week in different Locales are just yet another way that trying to use the WEEK_OF fields in a calendar goes haywire.

Affe
  • 47,174
  • 11
  • 83
  • 83
  • I think you nailed it, although it still doesn't explain why it works on Windows, as claimed in the other comments (I haven't tested it on Windows myself). – Jeshurun Jul 17 '11 at 08:16
  • Looking at the actual current real world time, I would guess that the person for whom it worked has a JVM that was still in last week at the time they tried it. – Affe Jul 17 '11 at 08:17
  • 2
    Which raises an additional question, why aren't I in bed :/ – Affe Jul 17 '11 at 08:19
3

There is Bug 4655637 (looks similar to your issue). I checked that code under latest JDK6 (Windows) and I have 15 in both cases. BTW: I am suggest to always use GregorianCalendar class explicitly unless you want something else (depending on your locale).

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
1

EDIT: official docs:

The following are the default combinations of the calendar fields. The most recent combination, as determined by the most recently set single field, will be used.

For the date fields:

 YEAR + MONTH + DAY_OF_MONTH
 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
 YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
 YEAR + DAY_OF_YEAR
 YEAR + DAY_OF_WEEK + WEEK_OF_YEAR

For the time of day fields:

 HOUR_OF_DAY
 AM_PM + HOUR

In addition to @Affe's clear answer, the following combinations seem to work (as of @GrzegorzSzpetkowski's bug report link)

Calendar expects the following combinations of the fields to determine a date.

 MONTH + DAY_OF_MONTH
 MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
 MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
 DAY_OF_YEAR
 DAY_OF_WEEK + WEEK_OF_YEAR

When you set DAY_OF_WEEK, the calendar expects a week field (WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH or WEEK_OF_YEAR) has also been set. So, avoid setting DAY_OF_WEEK without setting one of the week fields.

serv-inc
  • 35,772
  • 9
  • 166
  • 188