javacalendarjava.util.calendar

Why is January month 0 in Java Calendar?


In java.util.Calendar, January is defined as month 0, not month 1. Is there any specific reason to that ?

I have seen many people getting confused about that...


Solution

  • It's just part of the horrendous mess which is the Java date/time API. Listing what's wrong with it would take a very long time (and I'm sure I don't know half of the problems). Admittedly working with dates and times is tricky, but aaargh anyway.

    Do yourself a favour and use Joda Time instead, or possibly JSR-310.

    EDIT: As for the reasons why - as noted in other answers, it could well be due to old C APIs, or just a general feeling of starting everything from 0... except that days start with 1, of course. I doubt whether anyone outside the original implementation team could really state reasons - but again, I'd urge readers not to worry so much about why bad decisions were taken, as to look at the whole gamut of nastiness in java.util.Calendar and find something better.

    One point which is in favour of using 0-based indexes is that it makes things like "arrays of names" easier:

    // I "know" there are 12 months
    String[] monthNames = new String[12]; // and populate...
    String name = monthNames[calendar.get(Calendar.MONTH)];
    

    Of course, this fails as soon as you get a calendar with 13 months... but at least the size specified is the number of months you expect.

    This isn't a good reason, but it's a reason...

    EDIT: As a comment sort of requests some ideas about what I think is wrong with Date/Calendar: