[英]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
?
OffsetDateTime
from a LocalDate
adding the maximum offset available (which happens to be -18:00
hours)LocalDate
创建一个OffsetDateTime
添加可用的最大偏移量(恰好是-18:00
小时)OffsetDateTime
gets correctly serialized to a JSON value of 2021-10-21T23:59:59.999999999-18:00
OffsetDateTime
正确序列化为 JSON 值2021-10-21T23:59:59.999999999-18:00
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.请考虑用您所知道的一切更新您的问题。
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
OffsetDateTime
即23: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.
考虑添加有关此的信息。
String
s equalString
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.