Java DateFormat format and parse gives unexpected result

不想你离开。 提交于 2019-12-13 20:26:12

问题


I'm getting unexpected output when running following code,

DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
Date date = df.parse("2012-06-23");
System.out.println(df.format(date));

Output : 2012-01-01

I'm expecting 2012-06-23 as output,what am i doing wrong ?


回答1:


yyyy should be all lowercase, try that and see if it makes a difference...




回答2:


Refer this Date & time in Java. You'll get the date format used

Use 'y' instead of 'Y'.

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse("2012-06-23");
System.out.println(df.format(date));


来源:https://stackoverflow.com/questions/11162404/java-dateformat-format-and-parse-gives-unexpected-result

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