views:

420

answers:

2

I can't see where I'm going wrong with the code below, its probably something obvious but I'm too blind to see it at this stage. I'm passing a date of "01/01/2009" to an instance of calendar. I then try and set the month to 2 for March and the output I see is

formatted: 01/01/2009

cal month: 2

cal.set( Calendar.MONTH, mth ); //mth = int 2

log.debug("formatted: " + formatter.format(cal.getTime()));
log.debug("cal month: "+Integer.valueOf(cal.get(Calendar.MONTH)).toString());

When I set the Calendar.DAY to the max value the date comes out as 31/01/2009

Why does my setting of the Month not take?

+2  A: 

I'll look into exactly what's going on, but the general rule of thumb is: don't use java.util.{Date,Calendar}. Use Joda Time instead... it's so much cleaner...

EDIT: I can't reproduce your issue at the moment. I'd still recommend using Joda, but if you could post a short but complete program to demonstrate the problem, we may be able to work out what's going wrong.

EDIT: Based on another comment, I wonder whether your formatter is wrong... are you really using "dd/mm/yyyy" rather than "dd/MM/yyyy"? "mm" means minutes, not months.

Jon Skeet
Thanks @Jon, I'll have a look at that.
MadMurf
Spot on, formatter was wrong, mea culpa.
MadMurf
A: 

Perhaps you wanted to use Calendar.DAY_OF_MONTH

attilla