DateTimeParseException: Text '1-Aug-2018' could not be parsed at index 2

74 views Asked by At

I need to process data in the form of a Tab-delimited file with dates in the form dd-MMM-yyyy, for example 1-Aug-2018 or 15-Sep-2023. My code is:

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern ("d-MMM-uuuu");
private LocalDateTime toDate (String raw) {
       LocalDateTime date = LocalDateTime.parse (raw, DATE_FORMATTER);
    return date;
}

My exception is:

Exception in thread "main" java.time.format.DateTimeParseException: Text '1-Aug-2018' could not be parsed at index 2

Research done:

  • Careful reading of Oracle Java documentation
  • Tried dd instead of d but that just moved the index to 0 instead of 2
  • Tried yyyy instead of uuuu (out of desperation, the problem clearly isn't there)
  • An hour trawling the Internet but the few examples of my pattern with DateTimeFormatter (instead of SimpleDate, doesn't anybody ever clean up the old garbage on the Internet?!) are using it to format output rather than parsing input.
  • Another hour trawling StackOverflow, which is even worse than the general Internet for obsolete solutions (although the answers are way more professional), but all the answers seem to involve different patterns to mine and I can't see any differences in the code

Any suggestions? Or directions to relevant answers?

0

There are 0 answers