如何在夏令时内验证本地日期时间?

How can I validate a local date time within daylight savings time?

Mar 12, 2017 02:39:00 "America/Chicago" 不存在。当我将日期和时间设置为此值时,它不会失败。时间设置为 Mar 12, 2017 03:39:00 一小时后。怎么通知我这个时间不存在。这是时间向前跳跃的方式

01:59:59
3:00:00

如您所见,02:39:00 将永远不会出现在这个日期。

这是我使用的代码

package com.company;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {

    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.of("America/Chicago");
        ZonedDateTime dateTimeStart = ZonedDateTime.of(2017, 1, 1, 15, 39, 0, 0, ZoneId.of("America/Chicago"));
        ZonedDateTime dateTimeStartUtc = dateTimeStart.withZoneSameInstant(ZoneOffset.UTC);

        ZoneId zoneIdDst = ZoneId.of("America/Chicago");
        ZonedDateTime dateTimeStartDst = ZonedDateTime.of(2017, 3, 12, 2, 39, 0, 0, ZoneId.of("America/Chicago"));
        ZonedDateTime dateTimeStartUtcDst = dateTimeStart.withZoneSameInstant(ZoneOffset.UTC);
        int y = 90;
    }
}

您的示例没有抛出异常,因为 ZonedDateTime.of(..) 调整了日期时间。 javadoc 状态

This creates a zoned date-time matching the input local date-time as closely as possible. Time-zone rules, such as daylight savings, mean that not every local date-time is valid for the specified zone, thus the local date-time may be adjusted.

您可以使用 ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId) 来执行验证。

Obtains an instance of ZonedDateTime strictly validating the combination of local date-time, offset and zone ID.

This creates a zoned date-time ensuring that the offset is valid for the local date-time according to the rules of the specified zone. If the offset is invalid, an exception is thrown.

您首先需要构建一个 LocalDateTime。然后,您将获得 ZoneOffset 对应于该本地日期时间的 ZoneId。然后你可以把这三个都提供给ofStrict.

例如,

ZoneId zoneId = ZoneId.of("America/Chicago");
LocalDateTime ldt = LocalDateTime.of(2017, 3, 12, 2, 39, 0, 0);
ZoneOffset zoneOffset = zoneId.getRules().getOffset(ldt);
ZonedDateTime zdt = ZonedDateTime.ofStrict(ldt, zoneOffset, zoneId);

会抛出

Exception in thread "main" java.time.DateTimeException: LocalDateTime '2017-03-12T02:39' does not exist in zone 'America/Chicago' due to a gap in the local time-line, typically caused by daylight savings
    at java.time.ZonedDateTime.ofStrict(ZonedDateTime.java:484)

这里是不抛出异常的验证。你只需要询问时区规则LocalDateTime是否在给定的时区内有效:

public static boolean isValid(LocalDateTime ldt, ZoneId zoneId) {
    return !zoneId.getRules().getValidOffsets(ldt).isEmpty();
}