我正在 Spring Boot 中开发 Rest API,我们正在使用出色的库 Joda Time。由于我的服务器配置为在 UTC 时区工作,因此无需在整个应用程序中使用 DateTime
其中有DateTimeZone
里面的信息。我们更喜欢使用 LocalDateTime
用于存储系统中的所有日期。
现在的问题是关于打印LocalDateTime
iOS 8601 格式。看一下下面的代码:
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // server timezone
DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); // yyyy-MM-dd'T'HH:mm:ss.SSSZZ
LocalDateTime createdAtLocalDateTime = LocalDateTime.now();
DateTime createdAtDateTime = user.getCreatedAt().toDateTime(DateTimeZone.UTC);
logger.info("DT: {}", fmt.print(createdAtDateTime));
logger.info("LDT: {}", fmt.print(createdAtLocalDateTime));
这会输出这样的结果:
DT: 2019-03-20T20:19:19.691Z
LDT: 2019-03-20T20:37:00.642
所以没有Z
在序列化的最后LocalDateTime
但有一个Z
当我们序列化DateTime
时采用 UTC 时区。
现在的问题:如何配置格式化程序以在序列化 Z
期间在末尾(此 LocalDateTime
字母)输出此时区信息实例。我知道它始终采用 UTC,但我们的一个消费库需要此时区信息,不幸的是我们无法更改其中的代码。
更好的问题:如何配置 Jackson ObjectMapper
序列化LocalDateTime
将此 Z
转换为 json最后的信息?
添加到格式化程序fmt.withZoneUTC()
不起作用。
最佳答案
您可以将文本文字附加到格式化程序:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.append(ISODateTimeFormat.dateTime())
.appendLiteral('Z')
.toFormatter();
关于java - 来自 Joda Time ISO 8601 格式的 LocalDateTime,没有时区信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55269912/