- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一段简单的代码来使用 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/
我在从两个 LocalDateTime 对象获取秒数时遇到问题。 // a timestammp for the time when the connection was established pu
我有字段 initiationDate,它由 ToStringSerializer 类序列化为 ISO-8601 格式。 @JsonSerialize(using = ToStringSerializ
我有一个初始日期和一个 cron 表达式。我怎样才能找到满足此 cron 表达式的下一个日期?。 String cronExpresion = "* * * * * *" LocalD
我需要做一个 LocalDateTime UTC 日期到另一个日期的转换 LocalDateTime考虑特定时区 tz 的变量。 在我的研究中,我找到了很多解决方案,但它们都转换了 LocalDate
这个问题在这里已经有了答案: Java 8: Difference between two LocalDateTime in multiple units (11 个答案) 关闭 6 年前。 我知道
给定日期时间和时间, LocalDateTime rightDateWrongTime = new LocalDateTime("2017-03-02T15:23:00.000");
将 LocalDateTime 转换为 UTC 中的 LocalDateTime。 LocalDateTime convertToUtc(LocalDateTime date) { //do
这是我的代码: private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss");
我想为一些沉迷于日期值的数据创建一个小型 GUI。所以我在 JavaFX 中使用 LocalDateTimeTextField。所以我将使用以下代码获取选定的时间: LocalDateTimeText
解析 DateTime 时出现异常。我在这里缺少什么 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("0DDDHHmmss");
我有 REST 网络服务公开资源和创建日期。它是用 Java 8 编写的 - 使用 LocalDateTime。 Jackson 2 正在将其序列化为: “创建日期”:[2016, 5, 19, 18
有什么简单的方法可以将 Java 8 的 LocalDateTime 转换为 Joda 的 LocalDateTime? 其中一种方法是将其转换为字符串,然后从该字符串创建 Joda 的 LocalD
我在 Java 8 中使用 LocalDateTime.now() 获取 LocalDateTime。但有时这个 now() 函数会以没有秒的格式返回时间给我。我认为这是因为秒为零,但我需要秒。 代码
Date与LocalDateTime和LocalDate互相转换思路 Date转LocalDateTime和LocalDate都可以通过Date先转换成Instant然后再转换成LocalDateTi
在本教程中,我们将为LocalDate和LocalDateTime类编写自定义序列化程序和反序列化程序。 LocalDateSerializer LocalDateTimeSerializer Loc
从 API 传递的字符串通常遵循格式 yyyy-MM-dd HH:mm:ss.SSS但是当时间戳中有尾随 0 时,它们会被截断,例如 2019-07-16 13:29:15.100转换为 2019-0
我在 Java 8 中使用 spring boot 2.2.6 和 Jackson 2.10.3。我在整个项目中使用 localdatetime 对象。 Jackson 无法正确解析 LocalDat
我有使用年份和一年中的某一天 (1-365/366) 加上一天中的时间的数据,例如 2018-338T14:02:57.47583,而不是年月日。 我正在尝试编写一个输入时间戳的函数,它通过一堆正则表
我正在尝试将以下日期时间字符串解析为 LocalDateTimeObject,但是我无法识别日期时间字符串的格式。 2021 年 10 月 9 日星期六 02:10:23 -0400 Stri
我尝试并失败(或部分成功)为 LocalDateTime 实现扩展功能 这是我的尝试: fun LocalDateTime.isNotBefore(other: ChronoLocalDateTime
我是一名优秀的程序员,十分优秀!