【问题标题】:Java DateTimeFormatter.ISO_OFFSET_DATE_TIME Returns different values in Java 9+ compared to Java 8Java DateTimeFormatter.ISO_OFFSET_DATE_TIME 在 Java 9+ 中返回与 Java 8 不同的值
【发布时间】:2020-01-07 10:13:53
【问题描述】:

运行以下代码时,我会在不同版本的 JDK 中得到不同的行为:

Java 8 中我得到:

2020-01-07T09:34:38.994Z

Java 11 中我得到:

2020-01-07T09:37:05.55126Z
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class MyClass {
    public static void main(String args[]) {
        ZonedDateTime now = ZonedDateTime.now();

        DateTimeFormatter isoOffsetDateTime = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

        String format = isoOffsetDateTime.format(now);

        System.out.println(format);
    }
}

https://www.jdoodle.com/online-java-compiler/ 中运行只是为了更轻松地快速交换 JDK

此更改是否记录在任何地方,因为我找不到任何内容和/或有人知道为什么会发生这种情况?我发现这是因为 DateTimeFormatter.ISO_OFFSET_DATE_TIMEZonedDateTime 的默认 Jackson 格式化程序。

【问题讨论】:

  • 这能回答你的问题吗? ZonedDateTime change behavior jdk 8/11
  • @amer 这似乎是一个不同的问题(与时区有关,而这与小数秒的精度有关)
  • @MarkRotteveel 好吧,我的错

标签: java


【解决方案1】:

格式化程序的行为没有改变,但是你正在格式化的东西已经改变了。

now() 方法返回的日期时间精度提高了。 JDK-8068730

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2018-06-09
    • 2022-11-27
    相关资源
    最近更新 更多