Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://download.oracle.com/javase/6/docs/api/constant-values.html#java.util.Calendar.JANUARY" rel="nofollow noreferrer"><code>Calendar.JANUARY</code></a> is actually <code>0</code>, not <code>1</code>.</p> <p>When you provide <code>01</code> for the month field in <code>set</code>, you're actually setting the month to February, which is why you get <code>02</code> when <code>SimpleDateFormat</code> renders it as <code>MM</code>.</p> <p>When you use any of the <code>Calendar.get/set</code> methods, you must take extra precautions to make sure that you are aware of this discrepancy between the "natural" 1-based indexing, and the more "awkward" <code>Calendar</code>'s 0-based indexing. Any time you're getting/setting the month of a <code>Calendar</code>, there's always this potential to cause a serious bug.</p> <p>This is just one of those really awkward design in <a href="http://java.sun.com/javase/6/docs/api/java/util/Calendar.html" rel="nofollow noreferrer"><code>Calendar</code></a> that leaves a lot to be desired. One of the better, more pleasant date/time API library available out there is <a href="http://joda-time.sourceforge.net/" rel="nofollow noreferrer">Joda Time</a>, so if at all possible, you may consider a switch to that library instead.</p> <h3>API links</h3> <ul> <li><a href="http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html#MONTH" rel="nofollow noreferrer"><code>Calendar.MONTH</code></a> - "Field number for <code>get</code> and <code>set</code> indicating the month. This is a calendar-specific value. The first month of the year [...] is <code>JANUARY</code> which is 0".</li> <li><a href="http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html#set%28int,%20int,%20int,%20int,%20int%29" rel="nofollow noreferrer"><code>Calendar.set(…, int month, …)</code></a> - "<code>month</code> - the value used to set the <code>MONTH</code> calendar field. Month value is 0-based. e.g., 0 for January."</li> </ul> <hr> <h3>On octal literals</h3> <p>Also, note that <code>01</code> is actually an octal literal (i.e. <a href="http://en.wikipedia.org/wiki/Octal" rel="nofollow noreferrer">base 8</a>). You shouldn't make a habit of prepending <code>0</code> to integer literals (<a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1" rel="nofollow noreferrer">§3.10.1</a>), because they can cause subtle bugs/errors unless you're very careful.</p> <p>For example, <code>int i = 09;</code> is an illegal Java code.</p> <pre><code> System.out.println(010); // prints 8, not 10 </code></pre> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/2398208/can-i-customize-syntax-highlighting-in-eclipse-to-show-octal-literals-differently">Can I customize syntax highlighting in Eclipse to show octal literals differently?</a></li> <li><a href="https://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever"> Octal number literals: when? why? ever? </a></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