Here's my code:
java.util.Date TODAY = new java.util.Date();
SimpleDateFormat SDF = new SimpleDateFormat( "YYYY-MM-DD" );
System.out.println ( SDF.format( TODAY ) );'
And the result is:
2015-02-33
But today's date is 2015-02-02!
What may be the reason behind this wrong output?
Your assumptions about the date format string are wrong, the output is correct.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Y
Week year will usually give incorrect results around new year.D
will give incorrect results from February. So your format appeared fine most of last month.You want to use the
yyyy
year anddd
day of the month.However, I suggest you migrate to JSR-310 which is built into Java 8 and available for earlier versions of Java. The same code is
prints
When format for SimpleDateFormat is specified as follows:
Week year here is not what you wanted. See what is week year.
Your today's date is 2015-02-02, which means that it is 32 days since the beginning of the year 2015 passed and your are on the 33 day. That is why you get date "2015-02-33".
To mean year (and not week year) and day in month change format to SimpleDateFormat("yyyy-MM-dd");