Java Code Examples for java.time.ZonedDateTime#ofLocal()

The following examples show how to use java.time.ZonedDateTime#ofLocal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZonedDateTimeTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Assert that calling {@link ZonedDateTime#ofLocal(LocalDateTime, ZoneId, ZoneOffset)} with
 * the first three parameters produces a sane result with the localDateTime, and offset equal
 * to the last two.
 */
private static void checkOfLocal(LocalDateTime localDateTime, ZoneId zone,
        ZoneOffset preferredOffset, LocalDateTime expectedDateTime, ZoneOffset expectedOffset) {
    ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(localDateTime, zone, preferredOffset);
    String message = String.format(" for ofLocal(%s, %s, %s) = %s, ",
            localDateTime, zone, preferredOffset, zonedDateTime);
    // Note that localDateTime doesn't necessarily equal zoneDateTime.toLocalDateTime(),
    // specifically when offset is not a valid offset for zone at localDateTime (or ever).
    assertEquals("zone" + message, zone, zonedDateTime.getZone());
    assertEquals("offset" + message, expectedOffset, zonedDateTime.getOffset());
    assertEquals("localDateTime" + message, expectedDateTime, zonedDateTime.toLocalDateTime());
}
 
Example 2
Source File: AstroStateTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private Planet getPlanet(String thingID) {
    LocalDateTime time = LocalDateTime.of(TEST_YEAR, TEST_MONTH, TEST_DAY, 0, 0);
    ZonedDateTime zonedTime = ZonedDateTime.ofLocal(time, ZONE_ID, null);
    Calendar calendar = GregorianCalendar.from(zonedTime);
    switch (thingID) {
        case (TEST_SUN_THING_ID):
            SunCalc sunCalc = new SunCalc();
            return sunCalc.getSunInfo(calendar, TEST_LATITUDE, TEST_LONGITUDE, null);
        case (TEST_MOON_THING_ID):
            MoonCalc moonCalc = new MoonCalc();
            return moonCalc.getMoonInfo(calendar, TEST_LATITUDE, TEST_LONGITUDE);
        default:
            return null;
    }
}
 
Example 3
Source File: ZonedDateTimeHandle.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public ZonedDateTime readResolve() {
    return ZonedDateTime.ofLocal(dateTime, ZoneId.of(zoneId), offset);
}
 
Example 4
Source File: ZonedDateTimeTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected = NullPointerException.class)
public void test_ofLocal_localDateTime_null() {
    ZonedDateTime.ofLocal(null, ZONE_VIENNA, OFFSET_P1);
}
 
Example 5
Source File: ZonedDateTimeTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected = NullPointerException.class)
public void test_ofLocal_zone_null() {
    ZonedDateTime.ofLocal(LDT_P1, null, OFFSET_P1);
}
 
Example 6
Source File: ExecutionTimeUnixIntegrationTest.java    From cron-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Test that a cron expression that only runs at a certain time that falls inside the DST start gap
 * does not run on the DST start day. Ex. 2:15 AM is an invalid local time for the America/New_York
 * time zone on the DST start days.
 */
@Test
public void testDSTGap() {
    final ZoneId zoneId = ZONE_ID_NEW_YORK;
    final CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX));
    // Run at 2:15 AM each day for March 7 to 14
    final ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("15 2 7-14 3 *"));

    // Starting at 12 AM March. 7, 2015
    ZonedDateTime date = ZonedDateTime.of(2015, 3, 7, 0, 0, 0, 0, zoneId);

    // For America/New_York timezone, DST starts at 2 AM local time and moves forward 1 hour
    // DST dates for 2015-2026
    final Map<Integer, LocalDate> dstDates = new HashMap<>();
    dstDates.put(2015, LocalDate.of(2015, Month.MARCH, 8));
    dstDates.put(2016, LocalDate.of(2016, Month.MARCH, 13));
    dstDates.put(2017, LocalDate.of(2017, Month.MARCH, 12));
    dstDates.put(2018, LocalDate.of(2018, Month.MARCH, 11));
    dstDates.put(2019, LocalDate.of(2019, Month.MARCH, 10));
    dstDates.put(2020, LocalDate.of(2020, Month.MARCH, 8));
    dstDates.put(2021, LocalDate.of(2021, Month.MARCH, 14));
    dstDates.put(2022, LocalDate.of(2022, Month.MARCH, 13));
    dstDates.put(2023, LocalDate.of(2023, Month.MARCH, 12));
    dstDates.put(2024, LocalDate.of(2024, Month.MARCH, 10));
    dstDates.put(2025, LocalDate.of(2025, Month.MARCH, 9));
    dstDates.put(2026, LocalDate.of(2026, Month.MARCH, 8));

    final ZoneOffset easternDaylightTimeOffset = ZoneOffset.ofHours(-4);
    final ZoneOffset easternStandardTimeOffset = ZoneOffset.ofHours(-5);
    for (int year = 2015; year <= 2026; year++) {
        final LocalDate dstDateForYear = dstDates.get(year);
        boolean isPastDSTStart = false;
        int dayOfMonth = 7;
        while (dayOfMonth < 15) {
            final LocalDateTime localDateTime = LocalDateTime.of(year, Month.MARCH, dayOfMonth, 2, 15);
            // skip the DST start days... 2:15 AM does not exist in the local time
            if (localDateTime.toLocalDate().isEqual(dstDateForYear)) {
                dayOfMonth++;
                isPastDSTStart = true;
                continue;
            }
            final ZoneOffset expectedOffset = isPastDSTStart ? easternDaylightTimeOffset : easternStandardTimeOffset;
            final ZonedDateTime expectedDateTime = ZonedDateTime.ofLocal(localDateTime, zoneId, expectedOffset);

            final Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(date);
            assert (nextExecution.isPresent());
            date = nextExecution.get();
            assertEquals(expectedDateTime, date);
            dayOfMonth++;
        }
    }
}