Weird behaviour of Calendar and DateFormat

Posted by Nejc on Stack Overflow See other posts from Stack Overflow or by Nejc
Published on 2012-11-07T10:56:00Z Indexed on 2012/11/07 11:00 UTC
Read the original article Hit count: 212

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about date