Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Here is the code I am using

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;
    }
}
like image 831
Luke101 Avatar asked Apr 04 '17 00:04

Luke101


People also ask

How do you test for DST?

2.2 DRILL STEM TESTS Although a DST can be performed in uncased hole (open hole) or in cased hole (perforation tests), the open hole test is more common. The tool assembly which consists of a packer, a test valve, and an equalizing valve, is lowered on the drill pipe to a position opposite the formation to be tested.

What is the function to offset the date for daylight saving?

IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

What is the rule for Daylight Savings Time?

State Legislative Update. The daylight saving time (DST) period in the U.S. begins each year on the second Sunday in March when clocks are set forward by one hour. They are turned back again to standard time on the first Sunday in November as DST ends.

What is Daylight Saving Time adjustments?

BeyondWords. Daylight saving time (DST) is a one-hour clock adjustment observed by most of the U.S. and Canada, as well as some other countries, that begins in March and ends in November. From November to March, those locations operate on standard time.


1 Answers

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.

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

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.

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.

For example,

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

would throw

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)
like image 52
Sotirios Delimanolis Avatar answered Sep 22 '22 23:09

Sotirios Delimanolis