SimpleDateFormat API displays wrong Date

Tried formatting few dates using SimpleDateFormat API

String[] dates={"18-01-2015","9-02-2015","21-03-2015"};
for(String s:dates){
    SimpleDateFormat format=new SimpleDateFormat("DD-MM-yyyy");
    Date date=format.parse(s);
    System.out.println(date);
}

Output:

Sun Jan 18 00:00:00 IST 2015
Fri Jan 09 00:00:00 IST 2015
Wed Jan 21 00:00:00 IST 2015

You could notice for all dates it displays JAN instead of FEB/MAR.

Jon Skeet
people
quotationmark

You want dd instead of DD when you construct the SimpleDateFormat. DD means "day of year", not "day of month". Every time SimpleDateFormat looks like it's doing the wrong thing, you should consult the documentation and check your pattern text really, really carefully - particularly capitalization.

(There are other things that can go wrong of course - I've got a blog post for the most common ones.)

people

See more on this question at Stackoverflow