0

I've got problem with passing date and time (12 hour format) in my LocalDateTime object. Here's what I've done:

LocalDateTime MyDate;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm a");
    MyDate.parse(date_frome_string, formatter)

When I'm trying to past text like:

2020-09-04 09:15 PM 

I've got following error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-09-04 09:15 PM' could not be parsed: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 09:15

When I'm passing:

2020-09-04 09:15 AM

It's all ok.

amtrax
  • 344
  • 3
  • 17
  • 1
    You're going to get this fixed, but also — your code should be prepared to handle `DateTimeParseException` and possibly _retry_ parsing with other formats because, while you are pasting what you want, people _**will**_ try to enter other formats. – Stephen P Nov 09 '20 at 20:11

1 Answers1

2

The format symbol HH explicitly means 24-hour time. Use hh instead for 12-hour time.

chrylis -cautiouslyoptimistic-
  • 72,004
  • 20
  • 117
  • 147