0

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Maxim
  • 227
  • 2
  • 14
  • Does `row.soldDate` return a String, as running this locally, exaclt what you have, but using `"18-Feb-23"` instead of `row.soldDate` it works how I would expect. Perhaps the `-` are a special character? – user2131323 Mar 10 '23 at 21:43
  • 1
    what a Déjà vu... I swear I've seen that format [before](https://stackoverflow.com/questions/75666814/java-datetimeformatter-18-feb-23-could-not-be-parsed#comment133490479_75666836)... "at index 0" suggest there is some *invisible* character at the beginning of the string (e.g. `\000`) – user16320675 Mar 11 '23 at 00:27
  • There is a typo in your code, and also we don’t know what `row` is. A [mre], please? (Note: reproducible.) – Ole V.V. Mar 11 '23 at 05:16
  • 2
    One way to check for invisible characters: `row.soldDate.chars().forEach(System.out::println);`. Expected output is 49 56 45 70 101 98 45 50 51. If you get something else than 49 first, it explains your error. – Ole V.V. Mar 11 '23 at 05:21

2 Answers2

1

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

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.

mi1000
  • 104
  • 9