I\'m having some trouble with Java\'s Calendar. I\'m parsing some data from a txt file, and need to create a date. After completion of the following code:
tmpYea
months are indexed 0-11 instead of 1-12.
0 = January
1 = February
...
11 = December
Use tmpMonth = value -1
instead.
The reason is quite simple: design fault in the Calendar API. That's why the JSR 310 is on its way in order to improve the java support for dates.
Technically, the authors of the class thought it was good to use only static fields. So what you need to do is to use the following:
calendar = ...
calendar.setMonth(Calendar.JANUARY);
They didn't think that people might need dynamic settings to a calendar, just like you need (and most of us, for that matters).
From the API:
month - the value used to set the MONTH time field. Month value is 0-based. e.g., 0 for January.
The month values go from 0 (January) to 11 (December). Try using ((int) tmpMonth) - 1 when setting the month to get December.
I believe the month's value starts at 0 rather than 1 so it interprets 0 as Jan, 1 as Feb ... and then Jan again as 12.
When you set the Calendar.MONTH field, it is zero-based. {January=0... December=11}