/** * Resolves the new local date-time using this zone ID, retaining the offset if possible. * * @param newDateTime the new local date-time, not null * @return the zoned date-time, not null */ private ZonedDateTime resolveLocal(LocalDateTime newDateTime) { return ofLocal(newDateTime, zone, offset); }
public static ZonedDateTime ofLocal(LocalDateTime localDateTime, ZoneId zone, ZoneOffset preferredOffset) { return ZonedDateTime.ofLocal(localDateTime, zone, preferredOffset); }
/** * Combines this date-time with a time-zone to create a {@code ZonedDateTime} * trying to keep the same local date and time. * <p> * This returns a {@code ZonedDateTime} formed from this date-time and the specified time-zone. * Where possible, the result will have the same local date-time as this object. * <p> * Time-zone rules, such as daylight savings, mean that not every time on the * local time-line exists. If the local date-time is in a gap or overlap according to * the rules then a resolver is used to determine the resultant local time and offset. * This method uses {@link ZonedDateTime#ofLocal(LocalDateTime, ZoneId, ZoneOffset)} * to retain the offset from this instance if possible. * <p> * Finer control over gaps and overlaps is available in two ways. * If you simply want to use the later offset at overlaps then call * {@link ZonedDateTime#withLaterOffsetAtOverlap()} immediately after this method. * <p> * To create a zoned date-time at the same instant irrespective of the local time-line, * use {@link #atZoneSameInstant(ZoneId)}. * To use the offset as the zone ID, use {@link #toZonedDateTime()}. * * @param zone the time-zone to use, not null * @return the zoned date-time formed from this date and the earliest valid time for the zone, not null */ public ZonedDateTime atZoneSimilarLocal(ZoneId zone) { return ZonedDateTime.ofLocal(dateTime, zone, offset); }
/** * Obtains an instance of {@code ZonedDateTime} from a local date-time. * <p> * 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. * <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, when clocks are set back, there are two valid offsets. * This method uses the earlier offset typically corresponding to "summer". * <p> * In the case of a gap, when 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 * @return the zoned date-time, not null */ public static ZonedDateTime of(LocalDateTime localDateTime, ZoneId zone) { return ofLocal(localDateTime, zone, null); }
int hour, int minute, int second, int nanoOfSecond, ZoneId zone) { LocalDateTime dt = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); return ofLocal(dt, zone, null);
/** * Returns a copy of this date-time with a different time-zone, * retaining the local date-time if possible. * <p> * This method changes the time-zone and retains the local date-time. * The local date-time is only changed if it is invalid for the new zone, * determined using the same approach as * {@link #ofLocal(LocalDateTime, ZoneId, ZoneOffset)}. * <p> * To change the zone and adjust the local date-time, * use {@link #withZoneSameInstant(ZoneId)}. * <p> * This instance is immutable and unaffected by this method call. * * @param zone the time-zone to change to, not null * @return a {@code ZonedDateTime} based on this date-time with the requested zone, not null */ @Override public ZonedDateTime withZoneSameLocal(ZoneId zone) { Jdk8Methods.requireNonNull(zone, "zone"); return this.zone.equals(zone) ? this : ofLocal(dateTime, zone, offset); }