Search code examples
javasimpledateformat

getting month name wrong with SimpleDateFormat


I am having problem with this small piece of code

SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");

String str = "2010-03-13 01:01:22";

Date date = sf.parse(str);

SimpleDateFormat f = new SimpleDateFormat("d MMM yyyy hh:mm aaa");

System.out.println(" Date " + f.format(date));   

Output :

 Date 13 Jan 2010 01:01 AM

The code seems to be fine but still I am getting month name wrong. Please help !! Thanks.


Solution

  • You are using minute instead of month in your pattern. It should be:

    yyyy-MM-dd HH:mm:ss