简体   繁体   English

为什么 OffsetDateTime 序列化/反序列化结果有差异?

[英]Why OffsetDateTime serializations/deserialization results have difference?

I make OffsetDateTime object follows way:我使OffsetDateTime object 遵循以下方式:

LocalDate date = LocalDate.of(2021, Month.OCTOBER, 22);
OffsetDateTime dateTime = date.atTime(OffsetTime.MAX);

I get after serialization in json:我在 json 序列化后得到:

2021-10-21T23:59:59.999999999-18:00

And after deserialization I get follow value:反序列化后,我得到以下值:

2021-10-22T17:59:59.999999999Z

Why did this happen?为什么会这样?

Why does the Z appear instead of time zone?为什么出现Z而不是时区? Why is the date changed from 2021-10-21 to 2021-10-22 ?为什么日期从2021-10-21更改为2021-10-22

What we know so far到目前为止我们所知道的

  1. You are creating an OffsetDateTime from a LocalDate adding the maximum offset available (which happens to be -18:00 hours)您正在从LocalDate创建一个OffsetDateTime添加可用的最大偏移量(恰好是-18:00小时)
  2. this OffsetDateTime gets correctly serialized to a JSON value of 2021-10-21T23:59:59.999999999-18:00OffsetDateTime正确序列化为 JSON 值2021-10-21T23:59:59.999999999-18:00
  3. when deserialized, the value (as String ) is 2021-10-22T17:59:59.999999999Z反序列化时,值(作为String )为2021-10-22T17:59:59.999999999Z

The critical part is not included so far: What happens between 2. and 3.?到目前为止还没有包括关键部分:在 2. 和 3. 之间会发生什么?
Please consider updating your question with everything you know about it.请考虑用您所知道的一切更新您的问题。

What we can derive我们可以得出什么

The values that appear incongruent are basically the same moment in time ( Instant ), but represented at an offset of -18:00 at serialization and represented in UTC ( +00:00 or simply Z ).出现不一致的值基本上是同一时刻( Instant ),但在序列化时以-18:00的偏移量表示,并以 UTC 表示( +00:00或简单的Z )。 Due to a difference of 18 hours between those moments and due to the fact you created an OffsetDateTime with OffsetTime.MAX (which is 23:59:59.999999999-18:00 , the maximum time of day at an offset of -18:00 ).由于这些时刻之间存在 18 小时的差异,并且由于您使用OffsetTime.MAX OffsetDateTime23:59:59.999999999-18:00 ,一天中的最大时间偏移为-18:00 ) .

That's why the result you get after deserialization is not wrong , but its representation may not be the desired one.这就是为什么反序列化后得到的结果没有错,但它的表示可能不是你想要的。

My guess is that an Instant is used at the sub-steps between 2. and 3. and the deserialization simply provides date and time in UTC only.我的猜测是Instant用于 2. 和 3. 之间的子步骤,并且反序列化仅提供 UTC 格式的日期和时间。

I wouldn't pass any time with a maximum offset to any API if it is not explicitly required.如果没有明确要求,我不会将最大偏移量传递给任何 API。 Is it in your situation?是你的情况吗? Consider adding information about that, too.考虑添加有关此的信息。

What we can do to make the time String s equal我们可以做些什么来使时间String s 相等

You can use a different possibility of creating the OffsetDateTime from the LocalDate , that is using the maximum time of day without an offset explicitly at UTC:您可以使用从LocalDate创建OffsetDateTime的另一种可能性,即使用一天中的最大时间,而不在 UTC 明确偏移:

OffsetDateTime dateTime = OffsetDateTime.of(date, LocalTime.MAX, ZoneOffset.UTC);

This would serialize to 2021-10-21T23:59:59.999999999Z , you could also represent it as 2021-10-21T23:59:59.999999999+00:00 or similar (I would stick to Z ) and deserialization should return the same value.这将序列化为2021-10-21T23:59:59.999999999Z ,您也可以将其表示为2021-10-21T23:59:59.999999999+00:00或类似的(我会坚持Z )并且反序列化应该返回相同的值.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Gson 进行 OffsetDateTime 反序列化 - OffsetDateTime deserialization using Gson 为什么指定Map的初始容量会导致后续序列化产生不同的结果? - Why does specifying Map's initial capacity cause subsequent serializations to give different results? ZonedDateTime 和 OffsetDateTime 之间有什么区别? - What's the difference between ZonedDateTime and OffsetDateTime? 将 OffsetDateTime 与 Spring Boot 和 MongoDB 一起使用会导致 MappingException - Usage of OffsetDateTime with Spring Boot and MongoDB results in MappingException 我必须从字符串中获取 OffsetDateTime - I have to get OffsetDateTime from a string 如何在 Micronaut 上更改 Java java.time.OffsetDateTime 的默认序列化/反序列化? - How to change the default serialization/deserialization for Java java.time.OffsetDateTime on Micronaut? 为什么这些字符串比较在 java 中有不同的结果? - Why these String comparison have different results in java? json反序列化有一些问题 - Have some problems with json deserialization 我在for和stream中有不同的结果,为什么? - I have different results in for and stream, why? 使用 Spring Boot 和 Jackson ObjectMapper 时 OffsetDateTime 属性的 JSON 序列化差异 - Difference in JSON Serialization of OffsetDateTime Attribute when using Spring Boot and Jackson ObjectMapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM