• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can't seem to get gredorian calender working right

 
Chaz Anderson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi the code below will result in this being printed to screen:

Today Year: 2005
Today Month: 8
Today Day: 13
Today Hour: 8
Today Minute: 59
Today date: 2005-8-13

Yet the actual date is 2005-9-13. Can anyone tell me why the month keeps comeing out wrong?

 
Tom Billings
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the month-field of Calendar is zero-based, Calendar.JANUARY == 0. Adding 1 should do the trick.

Why, I'm not sure of.
 
Chaz Anderson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is only the month 0 based?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Date and Calendar classes the month is defined to be in the interval [0..11], not [1..12] as it in the real world. So month 8 is September, not August.

why is only the month 0 based?


Because it would be very confusing to redefine days or years (which are numbers) to use different ones. Months at least have a non-numerical designation (i.e., a name) which is unique. Of course, it is still very confusing.
Another explanation would be that Java array indices start at 0, and the names of months are stored in an array internally somewhere, so a Sun developer who wrote these classes got a bit lazy, and/or thought that it makes sense this way.
[ September 13, 2005: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic