4

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"
Itération 122442
  • 2,644
  • 2
  • 27
  • 73
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 31 '20 at 13:24
  • Your example, I can work my way through it, but it’s really neither completely minimal nor completely reproducible. Please reread [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ole V.V. Jan 31 '20 at 13:26
  • While `E` works fine for day of week abbreviation, a month may be either numerical or text. Therefore `M` and `MM` denote month *number* in either one or two digits. For month *abbreviation* like `Dec` you need three letters: `MMM`. – Ole V.V. Jan 31 '20 at 13:40

4 Answers4

3

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.

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
2

Date format should be

EEE MMM dd HH:mm:ss z yyyy

Your code works fine using this format.

using java.time API

LocalDate.parse(datestr, DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy")).format("TO DATE PATTERN");

Further details at Using java.time package to format date

Smile
  • 3,832
  • 3
  • 25
  • 39
2

Please find the code snippet below to solve your problem. The issue was the letter codes were correct, but there was character count mismatch , hence causing the issue. E.g.:Sun has three chars, but you were using a single E in your formatter.

public class Examp167 {
    public static 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));
    }
    public static void main(String[] args) throws Exception{
        String exDate  = "Sun Dec 29 00:24:09 CET 2019";
       System.out.println( formatDate(exDate, "EEE MMM dd HH:mm:ss zzz yyyy", "dd-MM-yyyy HH:mm:ss"));
    }
} 
akshaya pandey
  • 997
  • 6
  • 16
2

Firs use DateTimeFormatter instead of an old outdated class, then you should set the Locale since the day and month names are in English and last the in format needs to be MMM instead of M for the month

public static String formatDate(String date, String fromFormat, String toFormat) throws Exception {
    DateTimeFormatter inFormatter = DateTimeFormatter.ofPattern(fromFormat, Locale.US);

    DateTimeFormatter outFormatter = DateTimeFormatter.ofPattern(toFormat, Locale.US);
    return outFormatter.format(inFormatter.parse(date));
}

Example:

String exDate = "Sun Dec 29 00:24:09 CET 2019";
String out = formatDate(exDate, "E MMM d HH:mm:ss z yyyy", "dd-MM-yyyy HH:mm:ss");
System.out.println(out);

29-12-2019 00:24:09

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Clearly my favourite answer. Suggesting java.time, the only answer explaining the cause of the error and additionally the only one pointing out the need for a locale. – Ole V.V. Jan 31 '20 at 13:45