1

I am trying for hours now to parse this String to LocalDate and I just can't find where my pattern is wrong.

 public void parseDate() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd'-'MMM'-'yy");
        LocalDate date = LocalDate.parse("05-Sep-20", formatter);
}

I get the following exception:

java.time.format.DateTimeParseException: Text '05-Sep-20' could not be parsed at index 3

It works fine when using the pattern dd'-'MMMM'-'yy or dd'-'MM'-'yy, but it just won't work for MMM

I used the single quotes around the dash, because otherwise I was getting a parsing exception at a different index.

I am using Java1.8 and java.time

scalderon
  • 95
  • 7
  • Does this answer your question? [java.time.format.DateTimeParseException: Text could not be parsed at index 3](https://stackoverflow.com/questions/44925840/java-time-format-datetimeparseexception-text-could-not-be-parsed-at-index-3) – andrewJames Sep 11 '20 at 00:17

1 Answers1

0

It could be your locale. Try this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd'-'MMM'-'yy", Locale.US);
LocalDate date = LocalDate.parse("05-Sep-20", formatter);

ref: https://stackoverflow.com/a/44928297

LeoYulinLi
  • 129
  • 6