Home > other >  How to convert "2019-04-24" to "24-Apr-2019" date format? [duplicate]
How to convert "2019-04-24" to "24-Apr-2019" date format? [duplicate]

Time:09-17

I am trying to convert "2019-04-24" to "24-Apr-2019" this but it is converting it as "24-Jan-19".

This is the code I am using:

public static String dateFormatConvert(String date, String srcDateFormat, String targetDataFormat) throws ParseException {
    DateFormat dateFormat = new SimpleDateFormat(srcDateFormat);
    Date srcDate = dateFormat.parse(date);
    DateFormat destDateFormat = new SimpleDateFormat(targetDataFormat);
    return destDateFormat.format(srcDate);
}

Calling it:

    String date1 = CommonUtil.dateFormatConvert("2019-04-24", "yyyy-MM-DD", "DD-MMM-YY"); -> This is one of the format used (DD-MMM-YY) out of many
    System.out.println(date1);

What's going wrong here?

CodePudding user response:

According to the Documentation.

D - Day in year

d - Day in month

The correct code would be:

date1 = dateFormatConvert("2019-04-24", "yyyy-MM-dd", "dd-MMM-yyyy");
  • Related