- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试反序列化这个属性时出现错误:
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime deliveryDate;
这是反序列化类:
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
if (parser.getCurrentToken().equals(JsonToken.VALUE_STRING)) {
String rawDate = parser.getText();
return LocalDateTime.parse(rawDate);
} else {
throw context.wrongTokenException(parser, JsonToken.VALUE_STRING, "Expected string.");
}
}
和序列化类:
public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.toString());
}
这是我得到的错误:
"timestamp":1513962011642,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Text '2017-12-22T16:00:00.874Z' could not be parsed, unparsed text found at index 23
你知道为什么吗?
谢谢!
最佳答案
进程作为 Instant
而不是 LocalDateTime
.
Instant.parse( "2017-12-22T16:00:00.874Z" )
不确定来自 JSON 的原始数据。如果您的输入数据是 1513962011642
,它似乎是一个纪元以来的计数,大概是一个 epoch reference date。 1970-01-01T00:00:00Z,UTC 时间 1970 年的第一刻。
Instant instant = Instant.ofEpochMilli( 1_513_962_011_642L ) ;
如果原始输入是2017-12-22T16:00:00.874Z
,直接解析为Instant
。该字符串在标准 ISO 8601 中格式。末尾的 Z
是 Zulu
的缩写,表示 UTC。
java.time 类在解析/生成字符串时默认使用标准格式。
Instant instant = Instant.parse( "2017-12-22T16:00:00.874Z" ) ;
A LocalDateTime
故意缺少任何 time zone 的概念或 offset from UTC ,所以它不代表一个实际时刻,也不是时间轴上的一个点。您错误地试图将您的值放入错误的类中,无法表示它是 UTC 值这一事实。
相反,将该输入处理为 Instant
对象。一个瞬间代表时间轴上的一个点 UTC分辨率为 nanoseconds .
关于java - 使用 Jackson 反序列化 LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47945123/
我在从两个 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
我是一名优秀的程序员,十分优秀!