File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calendar Class (Wrong month! ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar Class (Wrong month! )" Watch "Calendar Class (Wrong month! )" New topic
Author

Calendar Class (Wrong month! )

Lilly Wang
Greenhorn

Joined: Jul 12, 2002
Posts: 4
Anybody know why the month that the Calendar class display is one month less than the current month?
e.g.
Calendar ca = Calendar.getInstance();
System.out.print( ca.get( Calendar.MONTH ) );
will display 6 instead of 7 (current month is July, it should be 7!)
Thanks in advance!
Ricardo Marques
Greenhorn

Joined: May 08, 2002
Posts: 27
I think it must be because of some historical reasons... Computers have always started counting from zero. The first index of an array is at 0 elements-offset, therefore at index 0.
For months it is the same. When January starts, no months have occured before, so it is at 0 months-offset.
That's why January is Zero. And December is Eleven!
This is merely speculation, but after a while one gets used to counting months from 0 to 11, and it starts to feel really weird if one has to write code lines where it isn't that way.


Sun Certified Programmer for the Java 2 Platform 1.4<p>If I can create, I will make mistakes. If I can learn with them, it must be some kind of art.
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
The MONTH field has values 0-11, so it is not wrong... just confusing.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

1st thing:
"Lilly"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
2nd thing:
Check out this blip of code, the Calendar.MONTH is off by one.

[ July 12, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Lilly Wang
Greenhorn

Joined: Jul 12, 2002
Posts: 4
Jessica,
Sorry! I have updated my display name now. And thanks!
Ricardo and Chad,
Thanks! That makes sense now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar Class (Wrong month! )