gpt4 book ai didi

time - 无法解析文本 : Unable to obtain LocalDateTime from TemporalAccessor:

转载 作者:行者123 更新时间:2023-12-01 22:08:43 24 4
gpt4 key购买 nike

我编写了一段简单的代码来使用 java 8 api 解析日期。我还解决了有关此主题的各种其他堆栈溢出问题,但未能解决错误。

package com.test.java8api;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, MM/DD/YYYY - HH:mm", Locale.ENGLISH).withResolverStyle(ResolverStyle.STRICT);
LocalDateTime date = LocalDateTime.parse("Sun, 04/22/2018 - 09:45",formatter);
System.out.println(date);
}

}

错误日志是

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Sun, 04/22/2018 - 09:45' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=4, DayOfYear=22},ISO resolved to 09:45 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.test.java8api.Test.main(Test.java:13)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {DayOfWeek=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=4, DayOfYear=22},ISO resolved to 09:45 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=7, WeekBasedYear[WeekFields[SUNDAY,1]]=2018, MonthOfYear=4, DayOfYear=22},ISO resolved to 09:45 of type java.time.format.Parsed
at java.time.LocalDate.from(LocalDate.java:368)
at java.time.LocalDateTime.from(LocalDateTime.java:456)
... 4 more

你能帮我解决这个问题吗?

编辑:

有人问我它是否可能是 Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8) 的副本

事实并非如此,因为上述问题讨论的是 LocalDate 和 LocalDateTime 的用法差异,而当前的问题是关于为模式使用正确的符号或字母表。

最佳答案

格式化模式区分大小写

"EEE, MM/DD/YYYY - HH:mm"

Read the documentation仔细了解格式化模式代码是区分大小写的。

  • DD 表示一年中的某一天。 dd 表示每月的第几天。
  • YYYY 表示基于周的年份。 yyyy(和 uuuu)表示日历年。

在发布到 Stack Overflow 之前请多加小心。您可能已经找到了数百个已发布在 Stack Overflow 上的工作代码示例,以显示代码中的错误。


提示:避免自定义格式,例如在您的问题中看到的格式。将日期时间值序列化为文本时,始终使用标准 ISO 8601格式。它们非常实用和有用,设计明确,易于机器解析,并且易于跨文化的人类阅读。

java.time 类在解析/生成字符串时默认使用 ISO 8601 格式。因此无需指定格式模式。

关于time - 无法解析文本 : Unable to obtain LocalDateTime from TemporalAccessor:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49963025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com