简体   繁体   English

Java Jsonb 在 ISO8601 中反序列化 UTC 日期时间

[英]Java Jsonb deserializing UTC datetime in ISO8601

I'm using JSON-B (yasson implementation) and I'm receiving data for an object with a field like this我正在使用 JSON-B(yasson 实现)并且我正在接收一个具有这样字段的对象的数据

{
  ...
  "timestamp": "2020-03-19T06:42:42Z",
  ...
}

which is perfectly standard ISO 8601 for a UTC date-time value.这是 UTC 日期时间值的完美标准 ISO 8601。 Now the corresponding Java class simply declares a Date member variable with no other specific annotation现在对应的 Java 类只声明了一个 Date 成员变量,没有其他特定的注解

...
 private Date timestamp;
...

everything seems to work fine and it looks like the JSON-B implementation correctly understands that as UTC without me having to specify a format using the @JsonbDateFormat annotation.一切似乎都很好,而且 JSON-B 实现似乎正确地将其理解为 UTC,而我不必使用@JsonbDateFormat注释指定格式。 I think I'm sure of it because I checked with我想我很确定,因为我检查过

ZonedDateTime datetimeCheck = ZonedDateTime.of(2020, 3, 19, 6, 42, 42, 0, ZoneId.of("UTC"));
Date parsedDateFromJson = myModel.getTimestamp();
boolean compareTs = parsedDateFromJson.equals(Date.from(datetimeCheck.toInstant()));

and it yields true however, when I ran another test removing the 'Z' from the date-time value I was expecting it to produce a different result interpreting the date-time value as local rather than UTC.然而,结果为,当我运行另一个测试从日期时间值中删除“Z”时,我期望它产生不同的结果,将日期时间值解释为本地而不是 UTC。 With my great surprise, the Date object obtained by JSON-B was exactly the same.出乎我的意料,JSON-B 获取的 Date 对象完全一样。 What am I missing here?我在这里缺少什么? Why are 2020-03-19T06:42:42Z and 2020-03-19T06:42:42 the same thing?为什么2020-03-19T06:42:42Z2020-03-19T06:42:42是一回事? (I don't think they are). (我不认为他们是)。 Or maybe is the JSON-B implementation considering UTC as default always when no timezone is specified?或者,当没有指定时区时,JSON-B 实现是否总是将 UTC 视为默认值?

Thanks谢谢

Or maybe is the JSON-B implementation considering UTC as default always when no timezone is specified?或者,当没有指定时区时,JSON-B 实现是否总是将 UTC 视为默认值?

Exactly this.正是这个。 Contrary to what its name implies, a java.util.Date actually doesn't represent a date but an instant in time.与其名称所暗示的相反, java.util.Date实际上并不代表日期,而是代表一个瞬间。 Specifically, it represents the number of milliseconds since UNIX epoch (January 1, 1970, 00:00:00 GMT).具体来说,它表示自 UNIX 纪元(1970 年 1 月 1 日,格林威治标准时间 00:00:00)以来的毫秒数。

2020-03-19T06:42:42 is a date + time without zone or offset information. 2020-03-19T06:42:42是没有区域或偏移信息的日期 + 时间。 When deserializing this date + time to a java.util.Date , ie an instant in time, you need to decide how to interpret the date + time.将此日期 + 时间反序列化为java.util.Date ,即瞬间,您需要决定如何解释日期 + 时间。 Is this the date + time at UTC?这是UTC的日期+时间吗? Is this the date + time in your local timezone?这是您当地时区的日期 + 时间吗?

Luckily for us, the JSON-B specification contains the following: "If not specified otherwise in this section, GMT standard time zone and offset specified from UTC Greenwich is used."对我们来说幸运的是,JSON-B 规范包含以下内容: “如果本节中没有另外指定,则使用 GMT 标准时区和从 UTC 格林威治指定的偏移量。” As this statement shows, the authors made the choice of interpreting a date + time without timezone as a date + time at UTC.正如此声明所示,作者选择将没有时区的日期 + 时间解释为 UTC 的日期 + 时间。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM