• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar Class (Wrong month! )

 
Lilly Wang
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chad McGowan
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The MONTH field has values 0-11, so it is not wrong... just confusing.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Lilly Wang
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic