Home > Net >  java.text.ParseException: Unparsable Date
java.text.ParseException: Unparsable Date

Time:11-12

I have this trash DateFormat in a log file that need to parse in order to compare it.

It’s in this format. Mon 03-Mai-21

My try:

    DateFormat format = new SimpleDateFormat("EEE FF-MMM-YY", Locale.GERMAN);
    String test = "Mon 03-Mai-21";
    Date date = null;
    try {
        date = format.parse(test);
    } catch (ParseException e) {
        e.printStackTrace();
    }

I’m getting a ParseException when trying to parse, but I have no clue what’s wrong with my pattern?

My expected result is 2021-05-03.

CodePudding user response:

java.time

I recommend that you use java.time, the modern Java date and time API, for your date work.

Declare a formatter for your format:

private static Map<Long, String> dayOfWeekAbbreviations
        = Map.ofEntries(
                Map.entry(1L, "Mon"),
                Map.entry(2L, "Die"),
                Map.entry(3L, "Mit"),
                Map.entry(4L, "Don"),
                Map.entry(5L, "Fre"),
                Map.entry(6L, "Sam"),
                Map.entry(7L, "Son"));

private static final DateTimeFormatter DATE_FORMATTER
        = new DateTimeFormatterBuilder()
                .appendText(ChronoField.DAY_OF_WEEK, dayOfWeekAbbreviations)
                .appendPattern(" dd-MMM-uu")
                .toFormatter(Locale.GERMAN);    

Parse like this:

    String test = "Mon 03-Mai-21";
    LocalDate date = LocalDate.parse(test, DATE_FORMATTER);
    System.out.println(date);

Output:

2021-05-03

Java assumes that the days of the week are abbreviated Mo., Di., Mi., Do., Fr., Sa., So. (at least my Java 11 does; I think there are Java version where the abbreviations come without the dots by default). Since your abbreviation was Mon for Montag (Monday), we needed to provide our own abbreviations. This is what I do through the map. I don’t know which abbreviations you are getting for the other days of the week, so please fill the right ones into the map yourself.

The other possible solution of course is to make sure the strings you are getting use Java’s abbreviations. Then you can use EEE in your format pattern string as in your question, which would make the formatter somewhat simpler.

What went wrong in your code?

There are three issues with what you tried:

  1. As I said, EEE in your format pattern string expects a two-letter day-of-week abbreviation like Mo. so cannot accept Mon. This caused the exception you saw.
  2. FF is for the “day of week in month”. So you were really trying to parse your string as the 3rd Monday of May 2021, which was why I asked whether you expected 17th of May. Lower case dd is for day of month.
  3. Upper case YY for year is wrong. Upper case Y is for week-based year and only useful with a week number. For year you would need lower case y with SimpleDateFormat. With DateTimeFormatter you may use u or y with a different meaning for years before year 1.

Links

  • Related