gpt4 book ai didi

java - 在夏令时前后将 Duration.ofDays(1) 和 Period.ofDays(1) 添加到 ZonedDateTime 之间的区别

转载 作者:行者123 更新时间:2023-12-02 06:47:52 25 4
gpt4 key购买 nike

夏令时在美国/东部时区 11 月 1 日凌晨 2 点结束。结果,凌晨 2 点变成了凌晨 1 点。

我无法理解下面给出的代码中的以下内容:

  1. 为什么第 2 行显示时间是 09:00,而不是 10:00(加上 1 天)?
  2. 为什么第 4 行显示时间 10:00,为什么不是 09:00(加上 24 小时)?

    LocalDateTime ld = LocalDateTime.of(2015, Month.OCTOBER, 31, 10, 0);

    ZonedDateTime date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 1 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Duration.ofDays(1));
    System.out.println(date); //line 2 - 2015-11-01T09:00-05:00[US/Eastern]

    date = ZonedDateTime.of(ld, ZoneId.of("US/Eastern"));
    System.out.println(date); //line 3 - 2015-10-31T10:00-04:00[US/Eastern]

    date = date.plus(Period.ofDays(1));
    System.out.println(date); //line 4 - 2015-11-01T10:00-05:00[US/Eastern]

有人可以帮我吗?

最佳答案

Duration:尽管 ofDays 方法 Duration 没有天的概念。 Duration.ofDays(1) 会立即转换为 24 小时,因此这就是您要添加的内容。由于您在 DST 结束前一天将 24 小时加到 10:00,因此您将在第二天获得 09:00,

Period:Duration 相反,Period 有天、周、月和年。因此,您要添加 1 个日历日,在第二天 (10:00) 到达相同的挂钟时间,即使这意味着 25 小时后(而不是 24 小时)。

关于java - 在夏令时前后将 Duration.ofDays(1) 和 Period.ofDays(1) 添加到 ZonedDateTime 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53287725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com