Java 8 DateTimeParseException when parsing AM/PM time with DateTimeFormatter

后端 未结 2 2083
青春惊慌失措
青春惊慌失措 2021-01-01 16:30

I\'m trying to use Java 8\'s java.time.format.DateTimeFormatter to parse a formatted string into a java.time.LocalTime object. However, I\'m runnin

相关标签:
2条回答
  • 2021-01-01 17:04

    You are using the wrong pattern, H is hour-of-day (0-23); you need h.

    The error is telling you that H is a 24 hour hour, and 8 is therefore obviously AM. Hence when you tell the parser to use 8 on the 24 hour clock and also tell the parse that it's PM it blows up.

    In short, read the documentation.

    0 讨论(0)
  • 2021-01-01 17:20

    The pattern symbol H stands for 24-hour-clock. So the parser tells you in case of "PM" that "08"-hour can only be in the scope of AM that is the first half of day.

    Solution: Use pattern symbol "h" instead (for 12-hour-clock).

    0 讨论(0)
提交回复
热议问题