javadatelocalizationdate-parsingdatetimeformatter

Java - DateTimeFormatter not working for German Date String


I'm trying to parse a date String in this format: "18-Feb-23". The month is in German. I've tried this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-LLL-yy", Locale.GERMAN);
LocalDate.parse(row.soldDate,formatter),

but I get this exception:

Caused by: java.time.format.DateTimeParseException: Text '18-Feb-23' could not be parsed at index 0

I also tried d-MMM-yy but that doesn't work.


Solution

  • Just tried out your code, works fine for me

    public void func () {
        String date = "18-Mai-23";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d-LLL-yy", Locale.GERMAN);
        LocalDate localDate = LocalDate.parse(date,formatter);
        System.out.println(localDate);
    }
    

    Here is the output

    2023-05-18
    

    Its very hard to guess from what you have shared but I think the string that is going to the date formatter in your case is impure . That is it contains some characters (that might not even be visible in output window) which are messing things up. Here are things you can try

    1. Instead of using data from "row.solDate", try to pass a hardcoded string. If that works, data in row.solDate is wrong
    2. Check if the encoding is similar, that is the encoding protocols like UTF-8 etc are similar across writing and reading part of your application.