无法将日期和时间转换为 LocalDateTime [Java]
Unable to Convert Date and Time to LocalDateTime [Java]
我正在尝试使用以下代码将字符串 Wed July 2019 10:53 PM
转换为 LocalDateTime
对象:
String dateAndTimeAsStr = "Wed July 2019 10:53 PM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMMM yyyy h:mm a");
LocalDateTime dateAndTimeAsLocalDateTime = LocalDateTime.parse(dateAndTimeAsStr, formatter);
然而,当我 运行 这段代码时,我得到以下错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed July 2019 10:53 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=3, MonthOfYear=7, Year=2019},ISO resolved to 22:53 of type java.time.format.Parsed
将 yyyy
更改为 YYYY
并将 h
更改为 hh
并没有产生任何不同的结果。
根据 this answer on SO and the documentation 看来我的模式与提供的文本匹配。
我做错了什么?
谢谢
失踪day-of-month
您输入的字符串缺少日期。它说 "July 2019" 但不是 7 月的 天 。
格式化的日期字符串是不可逆的(因为您可以使用格式化程序格式化现有的 LocalDateTime,但无法将其解析回来)。因为它缺少日期值。
我正在尝试使用以下代码将字符串 Wed July 2019 10:53 PM
转换为 LocalDateTime
对象:
String dateAndTimeAsStr = "Wed July 2019 10:53 PM";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMMM yyyy h:mm a");
LocalDateTime dateAndTimeAsLocalDateTime = LocalDateTime.parse(dateAndTimeAsStr, formatter);
然而,当我 运行 这段代码时,我得到以下错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Wed July 2019 10:53 PM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=3, MonthOfYear=7, Year=2019},ISO resolved to 22:53 of type java.time.format.Parsed
将 yyyy
更改为 YYYY
并将 h
更改为 hh
并没有产生任何不同的结果。
根据 this answer on SO and the documentation 看来我的模式与提供的文本匹配。
我做错了什么?
谢谢
失踪day-of-month
您输入的字符串缺少日期。它说 "July 2019" 但不是 7 月的 天 。
格式化的日期字符串是不可逆的(因为您可以使用格式化程序格式化现有的 LocalDateTime,但无法将其解析回来)。因为它缺少日期值。