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

问题描述 投票:2回答:1

[在华盛顿特区,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类来存储本地日期/时间?

java datetime localtime
1个回答
3
投票

使用注释中已经建议的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]
© www.soinside.com 2019 - 2024. All rights reserved.