Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that you're starting off with a calendar on January 30th 2013.</p> <p>You're then setting the year to 2013 - that's not a problem.</p> <p>You're then setting the month to 1 (i.e. February). What do you expect to happen here? What actually happens is that it will remember that it needs to set the month to 1, but <em>not</em> recompute the actual time value. The time value <em>will</em> be recomputed on your call to <code>get</code> though, as per the <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html" rel="noreferrer">documentation</a> (emphsis mine):</p> <blockquote> <p>set(f, value) changes calendar field f to value. In addition, it sets an internal member variable to indicate that calendar field f has been changed. Although calendar field f is changed immediately, the calendar's time value in milliseconds is not recomputed until the next call to get(), getTime(), getTimeInMillis(), add(), or roll() is made. Thus, multiple calls to set() do not trigger multiple, unnecessary computations. As a result of changing a calendar field using set(), other calendar fields may also change, depending on the calendar field, the calendar field value, and the calendar system. <strong>In addition, get(f) will not necessarily return value set by the call to the set method after the calendar fields have been recomputed.</strong> The specifics are determined by the concrete calendar class.</p> </blockquote> <p>When you try to change "January 30th" to "February 30th" and force a computation, what <em>actually</em> happens is that you end up on March 2nd on my box - but it may differ on your implementation.</p> <p>The best fixes are:</p> <ul> <li>Use <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29" rel="noreferrer"><code>Calendar.set(year, month, date)</code></a> instead to avoid this ordering issue</li> <li>Use <a href="http://joda-time.sf.net" rel="noreferrer">Joda Time</a> as a more sensible API in the first place.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload