问题
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