14

why does the following return 0?

int currMonth = c.get(Calendar.MONTH);
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
dr85
  • 733
  • 3
  • 13
  • 19

3 Answers3

55

The Months are numbered from 0 (January) to 11 (December).

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
12

Not natural indexing, but unnatural thinking of Sun people. :-)

Axel Fontaine
  • 34,542
  • 16
  • 106
  • 137
  • 1
    I'm wondering: should I upvote this or flag as offensive? I'll go for neither – Sean Patrick Floyd Jan 14 '11 at 18:15
  • No, I don't think so. Why should January be represented **numerically** as 1 (I'm not talking about textually)? Sure the String representation can be 1 in many the way many cultures write out dates, but that's handled by date formatters, not Calendar. 1+ rep for the smiley though. – Hovercraft Full Of Eels Jan 14 '11 at 18:15
  • 1
    Maybe it's just me, but I find mapping 1 (numeric) to 1 (text) just a wee bit easier... :-) – Axel Fontaine Jan 14 '11 at 18:20
  • I think that its definitely right to start it from 0 to conform with other indexing methods in Java. It was a dumb question on my behalf really - sorry to waste expert time! – dr85 Jan 14 '11 at 18:41
  • 1
    @David, it is not a dumb question, as the semantics are very confusing. I do however disagree with you. 0 really is the source of a lot of confusion. – Axel Fontaine Jan 14 '11 at 18:47
0

oops! sorry - I figured it out. Natural Indexing of course!!!

dr85
  • 733
  • 3
  • 13
  • 19
  • You don't use numbers for months but should use the Calendar constants such as Calendar.JANUARY (which as you're finding out is 0). The API shows this. – Hovercraft Full Of Eels Jan 14 '11 at 18:10
  • 3
    Calendar constants don't make any sense if you're trying to get the month number. In that case one must just remember to increment the return value. I find it quite annoying, especially since days are returned starting with 1... – Brian Knoblauch Jan 14 '11 at 18:15
  • yeah that is a bit stranfe... – dr85 Jan 14 '11 at 18:43