getting month name wrong with SimpleDateFormat

大兔子大兔子 提交于 2019-12-23 12:43:08

问题


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.


回答1:


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

yyyy-MM-dd HH:mm:ss



回答2:


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

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


来源:https://stackoverflow.com/questions/5497493/getting-month-name-wrong-with-simpledateformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!