java.time.LocalDateTime,多个时间线?

标签 java datetime localtime

2019 年 11 月 3 日凌晨 2:00,华盛顿特区的时钟“向后移动”至凌晨 1:00。

这意味着本地时间是这样的:

=== Nov 2nd ===:=== Nov 3rd ===========
               :                        
   9   10  11  12  1   2
---+---+---+---+---+-*-+
               :   +-#-+---+---+---+---
               :   1   2   3   4   5

所以,如果事故发生在凌晨 1:30,当我记录它时,应该清楚它是发生在第一时间线(那里的 *)还是第二时间线(#)。这对于确定责任和其他法律责任非常重要。

当查看java.time.LocalDateTime时,它似乎不包含时间线。这是故意的还是疏忽?

如果不包含时间线,是否有更好的Java类来存储本地日期/时间?

最佳答案

按照评论中的建议使用 ZonedDateTime,您可以尝试使用基本时间并添加一些分钟以查看结果,如下所示:

public static void main(String[] args) {
    // let's take a base time of 1:30 in EST (with daylight saving)
    ZonedDateTime nov3rd2019OneThirtyAM = ZonedDateTime
            .of(2019, 11, 3, 1, 30, 0, 0, ZoneId.of("America/New_York"));
    // add 10 minutes 10 times and print the result each time
    for (int i = 0; i < 100; i += 10) {
        System.out.println(nov3rd2019OneThirtyAM.plusMinutes(i));
    }
}

输出是这样的(检查10分钟加到1:50的部分,有2个):

2019-11-03T01:30-04:00[America/New_York]
2019-11-03T01:30-04:00[America/New_York]
2019-11-03T01:40-04:00[America/New_York]
2019-11-03T01:50-04:00[America/New_York]
2019-11-03T01:00-05:00[America/New_York]
2019-11-03T01:10-05:00[America/New_York]
2019-11-03T01:20-05:00[America/New_York]
2019-11-03T01:30-05:00[America/New_York]
2019-11-03T01:40-05:00[America/New_York]
2019-11-03T01:50-05:00[America/New_York]
2019-11-03T02:00-05:00[America/New_York]

关于java.time.LocalDateTime,多个时间线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60741448/

相关文章:

java - 为什么我可以将 -Xmx 设置为大于 Windows 和 Solaris 机器上的物理和虚拟内存的值?

java - 在 OpenGL ES 2.0 中绘制球体

php - 无法转换属性路径 : Expected a\DateTime or\DateTimeInterface 的值

php - 如何处理服务器时间和用户本地时间之间的差异?

c# - 将本地时间(10 位数字)转换为可读的日期时间格式

python-3.x - python中人类可读的时区?

java - 在 JfreeChart 中绘制模糊阴影?

java - 从 jar 文件连接到 XML 文件

c++ - 如何从字符串中解析日期/时间?

c# - 为什么 LocalTime 显示服务器时间而不是浏览器时间?