Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Joda Time ISO 8601格式的LocalDateTime,不含时区信息_Java_Spring Boot_Jackson_Jodatime - Fatal编程技术网

Java Joda Time ISO 8601格式的LocalDateTime,不含时区信息

Java Joda Time ISO 8601格式的LocalDateTime,不含时区信息,java,spring-boot,jackson,jodatime,Java,Spring Boot,Jackson,Jodatime,我正在SpringBoot中开发RESTAPI,我们正在使用奇妙的库Joda时间。由于我的服务器配置为在UTC时区工作,因此不需要在整个应用程序中使用DateTime,其中包含DateTimeZone信息。我们更喜欢使用LocalDateTime来存储系统中的所有日期 现在的问题是如何以IOS 8601格式打印LocalDateTime。请看下面的代码: TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // server timezone Da

我正在SpringBoot中开发RESTAPI,我们正在使用奇妙的库Joda时间。由于我的服务器配置为在UTC时区工作,因此不需要在整个应用程序中使用
DateTime
,其中包含
DateTimeZone
信息。我们更喜欢使用
LocalDateTime
来存储系统中的所有日期

现在的问题是如何以IOS 8601格式打印
LocalDateTime
。请看下面的代码:

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
因此,序列化
LocalDateTime
时,末尾没有
Z
,但在UTC时区序列化
DateTime
时,有一个
Z

现在的问题是:在序列化
LocalDateTime
实例期间,如何配置格式化程序以在末尾输出此时区信息(此
Z
字母)。我知道它总是以UTC为单位,但我们的一个消费库希望获得此时区信息,不幸的是,我们无法更改其中的代码

更好的问题:如何配置Jackson
ObjectMapper
LocalDateTime
序列化为json,并在末尾使用此
Z
信息


添加到格式化程序
fmt.withZoneUTC()
不起作用。

您可以将文本文字附加到格式化程序:

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .append(ISODateTimeFormat.dateTime())
        .appendLiteral('Z')
        .toFormatter();

编写一个自定义序列化程序/反序列化程序,在字符串中添加/删除Z。并看看即时类,它可能更符合您的需要谢谢您的评论。瞬间是安静的好。它适用于所描述的场景,但当它存储到MongoDb时,它的存储方式如下:{“iMillis”:NumberLong(1553117383722)}。我们还希望将信息存储在IOS 8601中的数据库中,您认为在序列化LocalDateTime对象后它会离开Z吗@shmosel@MS90当然,这是一个很好的答案。我可以假设
Z
总是在末尾,因为服务器总是在UTC下工作。非常感谢。似乎也没有时区信息的
Instant
在末尾用
Z
序列化,所以我不明白为什么
LocalDateTime
不包含它。这不一致,看起来像个bug。@MarcinKapusta这不是bug。LocalDateTime没有时区,因此包含
Z
是没有意义的。但是
Instant
也没有时区,并且在序列化过程中,
Z
显示在末尾:)