Weird behaviour of Calendar and DateFormat
- by Nejc
I encountered really strange behaviour when constructing a Calendar object and then formating it in a particular style.
Let the code do the talking:
public class Test
{
public static void main(String[] args)
{
SimpleDateFormat frmt = new SimpleDateFormat();
frmt.applyPattern("yyyy-MM-dd");
GregorianCalendar date = new GregorianCalendar(2012,1,1);
System.out.println(frmt.format(date.getTime()));
}
}
The output is:
2012-02-01
The expected output is of course:
2012-01-01
What am I doing wrong?