Thursday, October 9, 2008

[JAVA] Calendar Months Start from Zero

I don't have time to make a big deal out of this but please tell me which month is the 12th one in the year. I was so stupid to think it was December. Thanks to the JAVAAPI guys I got enlightened. The 12th month is Janaury cause the count starts from 0, it goes to 11 and then starts over again. Grand job to the java.util.Calendar guy!

14 comments:

Anonymous said...

Unless there's a good reason to do otherwise computers should always start counting at 0. Atleast for its internal representation.

There should be a display class that renders the date in big-edian (2008-12-01 for dec 1st) little-edian (01-12-2008) and brain-dead but commonly used in the USA (middle-edian) 12/01/2008.

But internally months should start counting at 0.

Tarelli said...

And how should day start counting? They start from 1, even the API guy thought that it would have been to much to make them start from 0 as for the confusion that it would have created. Months are equally referenced by men either by name or by number so the confusion is there anyway.

void said...

I think there is a very good reason for it to start at 1. Because that's the way people count time! Maybe internally it should start from 0, if you look at all the counters in (most) computer languages. But for someone who is a user of the language, what is the point on confusing him with something unintuitive?

Anonymous said...

the same thing is in Java, its just a bug with api design. Java is trying to fix that with new Date API.

It is just stupid mistake propagated through languages.

Anonymous said...

I'm pretty sure it's neither 12 nor 11, but is Calendar.DECEMBER. Then you don't have to worry about it.

Tarelli said...

You cannot pass a date in numeric format typed in by the user to the Calendar. End of the story.

Alex Miller said...

This is one of the (many, many) problems that will be addressed in the overhaul of the Java date apis with JSR 310 in Java 7.

Lots o' links here:

Java 7 JSR 310 links

Unknown said...

My Java API gives me constants for months, doesn't yours?
cal.set(Calendar.YEAR, 2008);
cal.set(Calendar.DATE, 3);
cal.set(Calendar.MONTH, Calendar.JULY);

Or are you doing this?

if ( cal.get(Calendar.MONTH) == 12 )

Bad boy!

Agreed, though, the month constants from 1 to 12 are kind of "common knowledge" and would be OK to write as literals. But usually using "magic numbers" is a code smell.

Btw, this is a legacy taken over from C (struct time_t).

Anonymous said...

The date api simply sucks. Use JODA. joda-time.sourceforge.net

Anonymous said...

I couldn't agree more in the use of Joda time instead. I have used the Calendar object heavily in the past to do some hard core date calcualtions and there are bugs in the way Calendar works particularly when jumping across daylight saving shifts, with the Calendar object you end up writing a lot of helper methods to do routine stuff. The Joda time API by contrast is much more intuitive to use.

Anonymous said...

I can only say, that it is annoying things like this that made me start developing the spiffy framework. Have a go at it on http://spiffyframework.sourceforge.net/

have fun
-K

Anonymous said...

There are other alternatives:

http://jodd.sourceforge.net/doc/jdatetime.html

I prefer that one.

Anonymous said...

"My Java API gives me constants for months, doesn't yours?"

Sure it does.

cal.set(Calendar.YEAR, 2008);
cal.set(Calendar.DATE, 32);
cal.set(Calendar.MONTH, Calendar.FEBRUARY);

Want to guess what this fine piece of constant using code does? Constants != win.

The current Date/Time API in Java is broken in tons of ways. Do what others have suggested and use Joda Time.

JuanM said...

Siempre tuve la misma duda al respecto.

Eventualmente podría comprender que los índices comiencen en cero,
pero... ¿Por qué los días comienzan en 1 (day starts in 1) y los meses en 0 (month starts in 0)?
No tiene sentido.

Si quiero obtener la fecha en formato dd/mm/yyyy, debo hacer:

int dd = calendario.get(Calendar.DAY_OF_MONTH);
int mm = calendario.get(Calendar.MONTH)+1; /*WTF?*/
int yyyy = calendario.get(Calendar.YEAR);