Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java unparsable date SimpleDateFormat [duplicate]

I have a date that looks like that:

Sun Dec 29 00:24:09 CET 2019

I have a little utility method that parses a string date from a format to another:

public String formatDate(String date, String fromFormat, String toFormat) throws Exception {
        SimpleDateFormat from = new SimpleDateFormat(fromFormat);
        SimpleDateFormat to = new SimpleDateFormat(toFormat);
        return to.format(from.parse(date));
    }

However, with above date format, I do not find the correct date pattern to indicate to my method. According to SimpleDateFormat patterns documentation, it should be (if I am not mistaken), the following (for Sun Dec 29 00:24:09 CET 2019):

"E M d HH:mm:ss z yyyy"

However, it throws the following exception:

java.text.ParseException: Unparseable date: "Sun Dec 29 00:24:09 CET 2019"
        at java.text.DateFormat.parse(DateFormat.java:366)
        at com.aptar.simulator.Utils.formatDate(Utils.java:60)

The method is called like this:

formatDate(exDate, "E M d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");

Where

exDate = "Sun Dec 29 00:24:09 CET 2019"
like image 241
Itération 122442 Avatar asked Jan 01 '23 08:01

Itération 122442


1 Answers

Try below solution -

formatDate("Sun Dec 29 00:24:09 CET 2019","EEE MMM d HH:mm:ss z yyyy","dd-MM-yyyy HH:mm:ss");

Format should be - "EEE MMM d HH:mm:ss z yyyy"

You should use EEE for Sun and MMM for Dec

hope this helps.

like image 111
Dipankar Baghel Avatar answered Jan 13 '23 17:01

Dipankar Baghel