Java Code Examples for java.time.zone.ZoneRules#getValidOffsets()

The following examples show how to use java.time.zone.ZoneRules#getValidOffsets() . 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: ZonedDateTime.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 2
Source File: ZonedDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 3
Source File: ZonedDateTime.java    From desugar_jdk_libs with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 4
Source File: TestZoneId.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
 
Example 5
Source File: ZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 6
Source File: ZonedDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 7
Source File: ZonedDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 8
Source File: TestZoneId.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
 
Example 9
Source File: TCKZoneRules.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
 
Example 10
Source File: TestZoneId.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
 
Example 11
Source File: ZonedDateTime.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 12
Source File: ZonedDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Obtains an instance of {@code ZonedDateTime} from a local date-time
 * using the preferred offset if possible.
 * <p>
 * The local date-time is resolved to a single instant on the time-line.
 * This is achieved by finding a valid offset from UTC/Greenwich for the local
 * date-time as defined by the {@link ZoneRules rules} of the zone ID.
 *<p>
 * In most cases, there is only one valid offset for a local date-time.
 * In the case of an overlap, where clocks are set back, there are two valid offsets.
 * If the preferred offset is one of the valid offsets then it is used.
 * Otherwise the earlier valid offset is used, typically corresponding to "summer".
 * <p>
 * In the case of a gap, where clocks jump forward, there is no valid offset.
 * Instead, the local date-time is adjusted to be later by the length of the gap.
 * For a typical one hour daylight savings change, the local date-time will be
 * moved one hour later into the offset typically corresponding to "summer".
 *
 * @param localDateTime  the local date-time, not null
 * @param zone  the time-zone, not null
 * @param preferredOffset  the zone offset, null if no preference
 * @return the zoned date-time, not null
 */
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
    Objects.requireNonNull(localDateTime, "localDateTime");
    Objects.requireNonNull(zone, "zone");
    if (zone instanceof ZoneOffset) {
        return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone);
    }
    ZoneRules rules = zone.getRules();
    List<ZoneOffset> validOffsets = rules.getValidOffsets(localDateTime);
    ZoneOffset offset;
    if (validOffsets.size() == 1) {
        offset = validOffsets.get(0);
    } else if (validOffsets.size() == 0) {
        ZoneOffsetTransition trans = rules.getTransition(localDateTime);
        localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
        offset = trans.getOffsetAfter();
    } else {
        if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
            offset = preferredOffset;
        } else {
            offset = Objects.requireNonNull(validOffsets.get(0), "offset");  // protect against bad ZoneRules
        }
    }
    return new ZonedDateTime(localDateTime, offset, zone);
}
 
Example 13
Source File: TestZoneId.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) {
    List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime);
    assertEquals(validOffsets.size(), type);
    assertEquals(rules.getOffset(dateTime), offset);
    if (type == 1) {
        assertEquals(validOffsets.get(0), offset);
        return null;
    } else {
        ZoneOffsetTransition zot = rules.getTransition(dateTime);
        assertNotNull(zot);
        assertEquals(zot.isOverlap(), type == 2);
        assertEquals(zot.isGap(), type == 0);
        assertEquals(zot.isValidOffset(offset), type == 2);
        return zot;
    }
}
 
Example 14
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 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 jdk8u-jdk 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: 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 17
Source File: ChronoZonedDateTimeImpl.java    From Bytecoder 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_jdk 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 19
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 20
Source File: ChronoZonedDateTimeImpl.java    From openjdk-jdk8u 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);
}