Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calendar in Java 5 - get(Calendar.MONTH) always returns 0 after setTime()

807589Jun 25 2008 — edited Jun 25 2008
Here is the code I've been playing with:

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
df.setLenient(false);
String testdate = "05/13/2003";
Date testp = null;
try {
testp = df.parse(testdate);
} catch (ParseException e) {
;
}
Calendar testCal = Calendar.getInstance();
testCal.setTime(testp);
int testMon = testCal.get(Calendar.MONTH);

No matter what value I set testdate above to, testMon is always 0. In the above code, it was supposed to be set to 5, right?

Something happens in setTime() to reset the Month field to 0 every time. I never noticed this in Java 1.4.

Has anyone else had similar experiences? I would also appreciate a correction if there is a bug in the above code.

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 23 2008
Added on Jun 25 2008
2 comments
448 views