Some issue with date formatter: MM/DD/YY not working as expected

309 views Asked by At

I am trying following piece of code:

    SimpleDateFormat formatter = new SimpleDateFormat("MM/DD/YY");
    Date d=formatter.parse("05/12/15");
    System.out.println(formatter.format(d));

Expected output: 05/12/15

Actual output: 12/362/15

3

There are 3 answers

0
thanuja On

The used pattern is wrong. As stated here DD returns the "day in year" whereas you need dd for the "day in month". Hence, the correct code is:

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy"); 
0
Laurentiu L. On

You are using uppercase D which is the day of the year. Use lower case d for day of the month. The year y should be lower case as well i.e "MM/dd/yy" .

See SimpleDateFormat documentation.

0
Zaw Than oo On

Learn details, SimpleDateFormat. For your format, use MM/dd/yy.

enter image description here