如何在夏令时内验证当地日期时间? [英] How can I validate a local date time within daylight savings time?

查看:192
本文介绍了如何在夏令时内验证当地日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2017年3月12日02:39:00America / Chicago不存在。当我将日期和时间设置为此值时,它不会失败。一小时后,时间设置为 2017年3月12日03:39:00 。如何通知此时间不存在。以下是时间向前跳过的方式

Mar 12, 2017 02:39:00 "America/Chicago" does not exist. When I set the date and time to this value it does not fail. The time gets set to Mar 12, 2017 03:39:00 an hour later. How can I be notified that this time does not exist. Here is how the time skips forward

01:59:59
3:00:00

如你所见 02:39:00 将永远不会存在这个日期。

As you can see 02:39:00 will never exist on this date.

这是我正在使用的代码

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;
    }
}


推荐答案

您的sample不会抛出异常,因为 ZonedDateTime.of(..)会调整日期时间。 javadoc 状态

Your sample doesn't throw an exception because ZonedDateTime.of(..) adjusts the date time. The javadoc states


这会创建一个与输入本地匹配的分区日期时间日期时间尽可能接近
。时区规则(例如夏令时)意味着
并非每个本地日期时间对指定区域有效,因此
可以调整本地日期时间。

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) 执行验证。

You could use ZonedDateTime#ofStrict(LocalDateTime, ZoneOffset, ZoneId) to perform the validation.


获取ZonedDateTime的实例,严格验证本地日期时间的
组合,偏移和区域ID。

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 。然后,您将获得该地方日期时间 ZoneId ZoneOffset 。然后你可以提供所有三个 ofStrict

You'll first need to construct a LocalDateTime. Then you'll get the ZoneOffset for your ZoneId for that local date time. Then you can provide all three to 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)

这篇关于如何在夏令时内验证当地日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆