Java - DateTimeFormatter not working for German Date String

170 Views Asked by At

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.

2

There are 2 best solutions below

0
Sakshum Sharma On BEST ANSWER

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.
0
mi1000 On

It's indeed possible that you have a BOM or something like this, you should print out each char to make sure your data is not wrong and verify that the encoding is correct, and, if not, then e.g. with Notepad++ convert this into a BOM-less file so that you won't meet this problem anymore.