无法解析日期 2020 年 4 月 15 日 12:14:17 AM 到 LocalDatetime

Unable to parse date Apr 15, 2020 12:14:17 AM to LocalDatetime

我正在尝试使用

将 2020 年 4 月 15 日 12:14:17 上午解析为 LocalDateTime
DateTimeFormatter.ofPattern("MMM DD',' YYYY h:mm:ss a")

我收到异常

    Exception in thread "main" java.time.format.DateTimeParseException: Text 'Apr 15, 2020 12:14:17 AM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MonthOfYear=4, WeekBasedYear[WeekFields[SUNDAY,1]]=2020, DayOfYear=15},ISO resolved to 00:14:17 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at com.paypal.Test.main(Test.java:76)

您的格式字符串只包含一个小时字符。应该是这样的:

 DateTimeFormatter.ofPattern("MMM dd, yyyy hh:mm:ss a")

编辑 我进一步修正了格式,使 DDYYYY 小写。此外,您不需要对逗号进行转义,因为它不是实际文本。

试试这种日期和年份小写的格式。

DateTimeFormatter.ofPattern("MMM dd',' yyyy hh:mm:ss a")