use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class ZoneRegion method ofId.
/**
* Obtains an instance of {@code ZoneId} from an identifier.
*
* @param zoneId the time-zone ID, not null
* @param checkAvailable whether to check if the zone ID is available
* @return the zone ID, not null
* @throws DateTimeException if the ID format is invalid
* @throws ZoneRulesException if checking availability and the ID cannot be found
*/
static ZoneRegion ofId(String zoneId, boolean checkAvailable) {
Objects.requireNonNull(zoneId, "zoneId");
checkName(zoneId);
ZoneRules rules = null;
try {
// always attempt load for better behavior after deserialization
rules = ZoneRulesProvider.getRules(zoneId, true);
} catch (ZoneRulesException ex) {
if (checkAvailable) {
throw ex;
}
}
return new ZoneRegion(zoneId, rules);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class ZonedDateTime method ofStrict.
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code ZonedDateTime} strictly validating the
* combination of local date-time, offset and zone ID.
* <p>
* 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.
*
* @param localDateTime the local date-time, not null
* @param offset the zone offset, not null
* @param zone the time-zone, not null
* @return the zoned date-time, not null
*/
public static ZonedDateTime ofStrict(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) {
Objects.requireNonNull(localDateTime, "localDateTime");
Objects.requireNonNull(offset, "offset");
Objects.requireNonNull(zone, "zone");
ZoneRules rules = zone.getRules();
if (rules.isValidOffset(localDateTime, offset) == false) {
ZoneOffsetTransition trans = rules.getTransition(localDateTime);
if (trans != null && trans.isGap()) {
// even though there are other kinds of gaps
throw new DateTimeException("LocalDateTime '" + localDateTime + "' does not exist in zone '" + zone + "' due to a gap in the local time-line, typically caused by daylight savings");
}
throw new DateTimeException("ZoneOffset '" + offset + "' is not valid for LocalDateTime '" + localDateTime + "' in zone '" + zone + "'");
}
return new ZonedDateTime(localDateTime, offset, zone);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class ChronoZonedDateTimeImpl method ofBest.
//-----------------------------------------------------------------------
/**
* 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);
}
}
// protect against bad ZoneRules
Objects.requireNonNull(offset, "offset");
return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class ChronoZonedDateTimeImpl method ofInstant.
/**
* 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);
// protect against bad ZoneRules
Objects.requireNonNull(offset, "offset");
LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>) chrono.localDateTime(ldt);
return new ChronoZonedDateTimeImpl<>(cldt, offset, zone);
}
use of java.time.zone.ZoneRules in project jdk8u_jdk by JetBrains.
the class ZonedDateTime method ofLocal.
/**
* 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 {
// protect against bad ZoneRules
offset = Objects.requireNonNull(validOffsets.get(0), "offset");
}
}
return new ZonedDateTime(localDateTime, offset, zone);
}
Aggregations