Java Calendar - Date is unpredictable after setting day_of_week
<p>I have the following code in a JUnit test, which seemed to work last week is failing this week:</p>

<pre><code>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));
</code></pre>

<p>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.</p>

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

<pre><code>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));
</code></pre>

<p>I get the output that I expect, 12 and 15.</p>

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