5

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.

4

2 回答 2

19

您在模式中使用分钟而不是月份。它应该是:

yyyy-MM-dd HH:mm:ss
于 2011-03-31T09:10:58.173 回答
8

mm代表分钟。MM解析月份时应该使用:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
于 2011-03-31T09:11:23.413 回答