• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with java.util.Calendar.set(...)  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following method is not returning the proper value for month. Did anyone face this problem? What is causing the Calendar Class to return wrong month?



The output is
 
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi neetu,

Calendar starts counting for month with zero. Probably nobody nows why on earth the API was designed to do that but it's the "correct" behavior for the Calendar class.

Marco
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar.MONTH is just a constant value (with the value 2) that indicates the month field, which you can use in certain methods of class Calendar. It is not a variable that contains the month number.

You can get the month that a calendar is set to with:

Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);

As Marco says, Calendar starts counting from 0 (unfortunately - this is one of the strange quirks of Java's date and time API). So 0 is January, 1 is February, etc., 11 is December.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaah, Calendar.MONTH was the problem?!? Unfortunately I didn't even think about this Thanks Jesper!
 
neetu shri
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
II have modified the system.out statements a little bit. As can be seen from the output when I say set(Calendar.MONTH, m) and do a get(Calendar.MONTH) then don't match. I get some months twice while some are missing. This is where I am lost.

The getTime method also returns some missing months and some repeated months. I see a pattern here when the month has 31 days.
Excuse me if there are any display formatting problems. I am uanble to preiew the message posting.
thanks again.



My output is :
13:47:07 INFO LoggerStream setMonth =0 ; getMonth =0 time Thu Jan 31 13:47:07
EST 2008
13:47:07 INFO LoggerStream setMonth =1 ; getMonth =2 time Sun Mar 02 13:47:07
EST 2008
13:47:07 INFO LoggerStream setMonth =2 ; getMonth =2 time Mon Mar 31 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =3 ; getMonth =4 time Thu May 01 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =4 ; getMonth =4 time Sat May 31 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =5 ; getMonth =6 time Tue Jul 01 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =6 ; getMonth =6 time Thu Jul 31 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =7 ; getMonth =7 time Sun Aug 31 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =8 ; getMonth =9 time Wed Oct 01 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =9 ; getMonth =9 time Fri Oct 31 13:47:07
EDT 2008
13:47:07 INFO LoggerStream setMonth =10 ; getMonth =11 time Mon Dec 01 13:47:0
7 EST 2008
13:47:07 INFO LoggerStream setMonth =11 ; getMonth =11 time Wed Dec 31 13:47:0
7 EST 2008
 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a Calendar object representing some time today (March 31) and you set that Calendar's month field to September, then since September only has 30 days, the result (September 31) is automatically repaired. That results in October 1.
 
neetu shri
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That explains it. Is there any best practice /recommendation on how to get the Month names for a given month number? like January for 0, February for 1,.... other than using an array with hardcoded months?
thanks again to all of you.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DateFormatSymbols class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!