Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28271
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The DateFormatSymbols class.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic