Java Code Examples for java.time.ZoneId#getRules()

The following examples show how to use java.time.ZoneId#getRules() . 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: TCKZoneIdSerialization.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 2
Source File: TCKZoneIdSerialization.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 3
Source File: TCKZoneIdSerialization.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 4
Source File: TCKZoneIdSerialization.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 5
Source File: TCKZoneIdSerialization.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 6
Source File: TCKZoneIdSerialization.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 7
Source File: TCKZoneIdSerialization.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test_deserialization_lenient_characters() throws Exception {
    // an ID can be loaded without validation during deserialization
    String id = "QWERTYUIOPASDFGHJKLZXCVBNM~/._+-";
    ZoneId deser = deserialize(id);
    // getId, equals, hashCode, toString and normalized are OK
    assertEquals(deser.getId(), id);
    assertEquals(deser.toString(), id);
    assertEquals(deser, deser);
    assertEquals(deser.hashCode(), deser.hashCode());
    assertEquals(deser.normalized(), deser);
    // getting the rules is not
    try {
        deser.getRules();
        fail();
    } catch (ZoneRulesException ex) {
        // expected
    }
}
 
Example 8
Source File: ChronoZonedDateTimeImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 9
Source File: ChronoZonedDateTimeImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 10
Source File: ChronoZonedDateTimeImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 11
Source File: ChronoZonedDateTimeImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 12
Source File: ChronoZonedDateTimeImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 13
Source File: ChronoZonedDateTimeImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 14
Source File: ChronoZonedDateTimeImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 15
Source File: ChronoZonedDateTimeImpl.java    From desugar_jdk_libs with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 16
Source File: ZoneIdFactory.java    From jphp with Apache License 2.0 5 votes vote down vote up
public static String aliasFor(ZonedDateTime dateTime) {
    String id = dateTime.getZone().getId();
    switch (id) {
        case "GMT":
        case "UTC":
            return id;
    }

    List<TimezoneWithAlias> aliases = idToAliases.get(id);
    if (aliases == null) {
        return null;
    }

    ZoneId zone = dateTime.getZone();
    ZoneRules rules = zone.getRules();
    boolean dst = zone.getRules().isDaylightSavings(dateTime.toInstant());
    ZoneOffset zoneOffset = rules.getOffset(dateTime.toInstant());

    int offset = zoneOffset.getTotalSeconds();

    for (TimezoneWithAlias alias : aliases) {
        if (alias.timezone.getOffset() == offset && alias.timezone.isDst() == dst) {
            return alias.alias.toUpperCase();
        }
    }

    return null;
}
 
Example 17
Source File: ChronoZonedDateTimeImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 18
Source File: ChronoZonedDateTimeImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
 
Example 19
Source File: ChronoZonedDateTimeImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from a local date-time using the preferred offset if possible.
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the zone identifier, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
static <R extends ChronoLocalDate> ChronoZonedDateTime<R> ofBest(
        ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
    List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(isoLDT);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = validOffsets.get(0);
        }
    }
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
 
Example 20
Source File: ChronoZonedDateTimeImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains an instance from an instant using the specified time-zone.
 *
 * @param chrono  the chronology, not null
 * @param instant  the instant, not null
 * @param zone  the zone identifier, not null
 * @return the zoned date-time, not null
 */
static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
    ZoneRules rules = zone.getRules();
    ZoneOffset offset = rules.getOffset(instant);
    Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
    ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>)chrono.localDateTime(ldt);
    return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}