In java.util.Calendar, why does setting the value of Calendar.YEAR affect the value of Calendar.MONTH?
<p>I was just trying out the java.util.Calendar method <code>getActualMaximum()</code> to see if I can get the maximum number of days in a month given the year. Here's my code:</p>

<pre><code>public static void main(String[] args) {
    Calendar c = Calendar.getInstance(); // today is July 29, 2012
    System.out.println(c.get(Calendar.MONTH) + " " + c.getActualMaximum(Calendar.DATE) + " " + c.get(Calendar.YEAR));
    c.set(Calendar.MONTH, Calendar.FEBRUARY); // set to February 2012
    System.out.println(c.get(Calendar.MONTH) + " " + c.getActualMaximum(Calendar.DATE) + " " + c.get(Calendar.YEAR));
    c.set(Calendar.YEAR, 2011); // set year to 2011, now February 2011
    System.out.println(c.get(Calendar.MONTH) + " " + c.getActualMaximum(Calendar.DATE) + " " + c.get(Calendar.YEAR));
}
</code></pre>

<p>The output I'm expecting is:</p>

<pre><code>6 31 2012 // last day of July 2012 is 31
1 29 2012 // last day of Feb 2012 is 29
1 28 2011 // last day of Feb 2011 is 28
</code></pre>

<p>However, this is what I'm getting:</p>

<pre><code>6 31 2012
1 29 2012
2 31 2011 // HUH?
</code></pre>

<p>(In java.util.Calendar, the month values start from 0 to 11 for January to December, while the days of the months themselves start with 1.)</p>

<p>Why was the month suddenly set to Calendar.MARCH? What's going on here, and what can I do to get the last days of a given month and year correctly? I'm exploring the method because I don't want to set up my own array of booleans where it's true for indices that represent 31-day months and false for not.</p>
