File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes I can't seem to get gredorian calender working right Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I can Watch "I can New topic
Author

I can't seem to get gredorian calender working right

Chaz Anderson
Greenhorn

Joined: Sep 13, 2005
Posts: 3
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

Joined: Sep 12, 2005
Posts: 16
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

Joined: Sep 13, 2005
Posts: 3
why is only the month 0 based?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
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 ]

Ping & DNS - my free Android networking tools app
 
 
subject: I can't seem to get gredorian calender working right