java - 来自 Joda Time ISO 8601 格式的 LocalDateTime,没有时区信息

标签 java spring-boot jackson jodatime

我正在 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/

相关文章:

java - 具有一个 namespace 和localpart的多个SOAP端点

java - 如何使用 Jackson 序列化/反序列化第三方 Java 对象

java - google-api-client 和 google-api-services 之间的版本兼容性

java - Spring 批处理中的重写 Bean 问题

javascript - 我的 ojet 应用程序无法在 mozilla firefox 或 Internet Explorer 中运行

json - 如何自定义 Jackson 类型信息机制

java - 无法在 Springboot "no single-String constructor/factory method"中使用 Jackson 反序列化自定义日期对象

java - 在面板中设置标签布局

java - Python 中的静态初始化器

java - LIBGDX Animate without enternig render()