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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With