首页 > 解决方案 > 无法将 2020 年 4 月 15 日上午 12:14:17 的日期解析为 LocalDatetime

问题描述

我正在尝试使用将 2020 年 4 月 15 日 12:14:17 AM 解析为 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)

标签: javajava-8localdate

解决方案


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

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

推荐阅读