Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 865
Dharmesh Avatar asked Mar 31 '11 09:03

Dharmesh


2 Answers

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

yyyy-MM-dd HH:mm:ss
like image 130
morja Avatar answered Sep 30 '22 00:09

morja


mm stands for minute. You should use MM when parsing month:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
like image 20
Peter Knego Avatar answered Sep 30 '22 00:09

Peter Knego