javadatesimpledateformat

Simple date format after formatting showing the month as 30 instead of correct month


I am having date object of "valueDate" "Wed May 08 05:30:00 IST 2013" in java.I am using simpleDateFormat of "yyyy-mm-dd" format.But after formatting i am getting result as "2013-30-08" instead "2013-05-08". What i am doing wrong. can anyone help me in this?

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");                   

String formattedValue= dateFormat.format(valueDate);

Solution

  • You have to use uppercase 'M' for month:

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
    

    As you can see in the documentation

    m is for minute
    M is for month