This discussion is archived
2 Replies Latest reply: Jun 25, 2008 5:40 PM by 807589 RSS

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

807589 Newbie
Currently Being Moderated
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!