Search code examples
javadatedatetimesimpledateformatsqldatetime

Invalid date while formating using simple date format


I have a date String Mon Feb 8 00:00:00 UTC+0530 1954. I want to convert it into the format DD/MM/YYYY. The output is always 08/38/1954 or 09/39/1954.

I have used the following code.

SimpleDateFormat format = new SimpleDateFormat("MM/DD/YYYY", Locale.US);
System.out.println(format.format(new Date(dateStr)));

Solution

  • D in SimpleDateformat stands for day in year where as d stands for day in month so replace DD with dd.

    similary replace YYYY with yyyy