Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar object January return 0?

 
Jack Wootton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MONTH seems to return 0 inn January.

Is December also 11. So adding 1 would be a solution?

Many thanks

Jack
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't rely on the exact return value. Instead, you should compare it with the constants provided with the Calendar class such as Calendar.JANUARY and Calendar.DECEMBER. Look at the appropriate Java API documentation for the version you are using.

Layne
 
Jack Wootton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't need to compare the date.....just make it look pretty. I asked since I'm developing in Sony E. T610 so thought the Calender class may have stripped down or changed.

Are you saying that i should compare the date returned with Calendar.JANUARY, and if it matches, then explicitly store '1'?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to store a month value internally as 1..12, then you should first calculate the offset from the start of your range to that returned by Calendar. Then you can add that to any value calculated using Calendar.That way you don't depend on any knowledge of Calendar.JANUARY other than that it is the first month, and I think that's a pretty safe assumption for the next few years at least.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that Calendar.JANUARY == 0 is explicitly guaranteed in the Calendar API, and has been since the beginning. See Calendar.MONTH. It's not going to change. (This, unfortunately is why Sun can never correct the icky choices they made in making the month field start at 0 while the day of month field starts at 1.) So go ahead, Jack - add 1, you'll get the value you expect.
 
Jack Wootton
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. If it's 0 then addnig 1 will do for me.

Many thanks

Jack
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are displaying the date, you should look at java.text.DateFormat and java.text.SimpleDateFormat. These classes come in handy when you are manipulating dates in code.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic