javasimpledateformatjava.util.date

java.util.Date is generating a wrong date?


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?


Solution

  • What may be the reason behind this Wrong Output ?

    Your assumptions about the date format string are wrong, the output is correct.

    y   Year
    Y   Week year
    D   Day in year
    d   Day in month
    M   Month in year
    m   Minute in hour
    

    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.