aspose file tools*
The moose likes Beginning Java and the fly likes Calendar object January return 0? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar object January return 0?" Watch "Calendar object January return 0?" New topic
Author

Calendar object January return 0?

Jack Wootton
Greenhorn

Joined: Dec 29, 2004
Posts: 6
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Jack Wootton
Greenhorn

Joined: Dec 29, 2004
Posts: 6
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Jack Wootton
Greenhorn

Joined: Dec 29, 2004
Posts: 6
Thank you. If it's 0 then addnig 1 will do for me.

Many thanks

Jack
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
 
 
subject: Calendar object January return 0?