Java DateTime 对象的标准(往返)字符串格式是什么

What is the standard (round trip) string format for Java DateTime objects

我有一个 REST 服务器(在 C# 中,Java 中的版本即将推出)有大量语言的客户端(加上 Swagger)。

我需要(反)序列化 LocalDateTime、ZonedDateTime 和 OffsetDateTime()。通过使用每个代码,我相信 toString() 为我提供了每个这些的 ISO 可往返文本。

对吗?

如果没有,我应该使用什么?

ps - 我们正在将任何日期时间减少到上面三个 类 之一。这应该可以处理任何用例。

关闭。

LocalDateTimeOffsetDateTime 从它们的 toString 方法序列化为 ISO 8601。他们总是可以通过他们的单参数 parse 方法解析生成的字符串。您的往返行程已完成。不过,这并不意味着它们解析每个 ISO 8601 变体。

对于 ZonedDateTime,java.time 的开发人员发明了他们自己的 ISO 8601 扩展:方括号中的时区 ID 通常以打印字符串结尾,它不是 ISO 8601 的一部分。例如,a ZonedDateTime 可以打印字符串 2020-07-09T20:58:09.445153+02:00[Europe/Zurich]2020-07-09T20:58:09.445153+02:00 部分是 ISO 8601。[Europe/Zurich] 部分不是。 Europe/Zurich 是一个 IANA 时区 ID,但是,很多语言应该都有机会处理它。如果您确实需要处理时区(不仅仅是 UTC 偏移),我怀疑是否有更好的选择。 ISO 8601 本身没有提供任何处理真实时区的方法。如果 ZonedDateTime 有一个偏移量作为其“时区”,则不会打印方括号中的 ID,因此在这种情况下整个字符串确实符合 ISO 8601。

你是对的,ISO 8601 是 日期和时间数据标准,推荐你使用。

文档引用

来自 LocalDateTime.toString():

The output will be one of the following ISO-8601 formats: …

OffsetDateTime.toString():

The output will be one of the following ISO-8601 formats: …

ZonedDateTime.toString():

The format consists of the LocalDateTime followed by the ZoneOffset. If the ZoneId is not the same as the offset, then the ID is output. The output is compatible with ISO-8601 if the offset and ID are the same.

Link: 相关问题: