/** * Returns a copy of this date-time with the zone ID set to the offset. * <p> * This returns a zoned date-time where the zone ID is the same as {@link #getOffset()}. * The local date-time, offset and instant of the result will be the same as in this date-time. * <p> * Setting the date-time to a fixed single offset means that any future * calculations, such as addition or subtraction, have no complex edge cases * due to time-zone rules. * This might also be useful when sending a zoned date-time across a network, * as most protocols, such as ISO-8601, only handle offsets, * and not region-based zone IDs. * <p> * This is equivalent to {@code ZonedDateTime.of(zdt.getDateTime(), zdt.getOffset())}. * * @return a {@code ZonedDateTime} with the zone ID set to the offset, not null */ public ZonedDateTime withFixedOffsetZone() { return this.zone.equals(offset) ? this : new ZonedDateTime(dateTime, offset, offset); }
/** * Returns a copy of this date-time with the zone ID set to the offset. * <p> * This returns a zoned date-time where the zone ID is the same as {@link #getOffset()}. * The local date-time, offset and instant of the result will be the same as in this date-time. * <p> * Setting the date-time to a fixed single offset means that any future * calculations, such as addition or subtraction, have no complex edge cases * due to time-zone rules. * This might also be useful when sending a zoned date-time across a network, * as most protocols, such as ISO-8601, only handle offsets, * and not region-based zone IDs. * <p> * This is equivalent to {@code ZonedDateTime.of(zdt.getDateTime(), zdt.getOffset())}. * * @return a {@code ZonedDateTime} with the zone ID set to the offset, not null */ public ZonedDateTime withFixedOffsetZone() { return this.zone.equals(offset) ? this : new ZonedDateTime(dateTime, offset, offset); }
/** * Resolves the offset into this zoned date-time. * <p> * This ignores the offset, unless it can be used in an overlap. * * @param offset the offset, not null * @return the zoned date-time, not null */ private ZonedDateTime resolveOffset(ZoneOffset offset) { if (offset.equals(this.offset) == false && zone.getRules().isValidOffset(dateTime, offset)) { return new ZonedDateTime(dateTime, offset, zone); } return this; }
/** * Resolves the offset into this zoned date-time. * <p> * This ignores the offset, unless it can be used in an overlap. * * @param offset the offset, not null * @return the zoned date-time, not null */ private ZonedDateTime resolveOffset(ZoneOffset offset) { if (offset.equals(this.offset) == false && zone.getRules().isValidOffset(dateTime, offset)) { return new ZonedDateTime(dateTime, offset, zone); } return this; }
/** * Obtains an instance of {@code ZonedDateTime} using seconds from the * epoch of 1970-01-01T00:00:00Z. * * @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z * @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999 * @param zone the time-zone, not null * @return the zoned date-time, not null * @throws DateTimeException if the result exceeds the supported range */ private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) { ZoneRules rules = zone.getRules(); Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond); // TODO: rules should be queryable by epochSeconds ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset); return new ZonedDateTime(ldt, offset, zone); }
/** * Obtains an instance of {@code ZonedDateTime} leniently, for advanced use cases, * allowing any combination of local date-time, offset and zone ID. * <p> * This creates a zoned date-time with no checks other than no nulls. * This means that the resulting zoned date-time may have an offset that is in conflict * with the zone ID. * <p> * This method is intended for advanced use cases. * For example, consider the case where a zoned date-time with valid fields is created * and then stored in a database or serialization-based store. At some later point, * the object is then re-loaded. However, between those points in time, the government * that defined the time-zone has changed the rules, such that the originally stored * local date-time now does not occur. This method can be used to create the object * in an "invalid" state, despite the change in rules. * * @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 */ private static ZonedDateTime ofLenient(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) { Jdk8Methods.requireNonNull(localDateTime, "localDateTime"); Jdk8Methods.requireNonNull(offset, "offset"); Jdk8Methods.requireNonNull(zone, "zone"); if (zone instanceof ZoneOffset && offset.equals(zone) == false) { throw new IllegalArgumentException("ZoneId must match ZoneOffset"); } return new ZonedDateTime(localDateTime, offset, zone); }
/** * Obtains an instance of {@code ZonedDateTime} leniently, for advanced use cases, * allowing any combination of local date-time, offset and zone ID. * <p> * This creates a zoned date-time with no checks other than no nulls. * This means that the resulting zoned date-time may have an offset that is in conflict * with the zone ID. * <p> * This method is intended for advanced use cases. * For example, consider the case where a zoned date-time with valid fields is created * and then stored in a database or serialization-based store. At some later point, * the object is then re-loaded. However, between those points in time, the government * that defined the time-zone has changed the rules, such that the originally stored * local date-time now does not occur. This method can be used to create the object * in an "invalid" state, despite the change in rules. * * @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 */ private static ZonedDateTime ofLenient(LocalDateTime localDateTime, ZoneOffset offset, ZoneId zone) { Jdk8Methods.requireNonNull(localDateTime, "localDateTime"); Jdk8Methods.requireNonNull(offset, "offset"); Jdk8Methods.requireNonNull(zone, "zone"); if (zone instanceof ZoneOffset && offset.equals(zone) == false) { throw new IllegalArgumentException("ZoneId must match ZoneOffset"); } return new ZonedDateTime(localDateTime, offset, zone); }
/** * Obtains an instance of {@code ZonedDateTime} using seconds from the * epoch of 1970-01-01T00:00:00Z. * * @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z * @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999 * @param zone the time-zone, not null * @return the zoned date-time, not null * @throws DateTimeException if the result exceeds the supported range */ private static ZonedDateTime create(long epochSecond, int nanoOfSecond, ZoneId zone) { ZoneRules rules = zone.getRules(); Instant instant = Instant.ofEpochSecond(epochSecond, nanoOfSecond); // TODO: rules should be queryable by epochSeconds ZoneOffset offset = rules.getOffset(instant); LocalDateTime ldt = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, offset); return new ZonedDateTime(ldt, offset, zone); }
Jdk8Methods.requireNonNull(zone, "zone"); if (zone instanceof ZoneOffset) { return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone); return new ZonedDateTime(localDateTime, offset, zone);
Jdk8Methods.requireNonNull(zone, "zone"); if (zone instanceof ZoneOffset) { return new ZonedDateTime(localDateTime, (ZoneOffset) zone, zone); return new ZonedDateTime(localDateTime, offset, zone);
/** * Returns a copy of this date-time changing the zone offset to the * earlier of the two valid offsets at a local time-line overlap. * <p> * This method only has any effect when the local time-line overlaps, such as * at an autumn daylight savings cutover. In this scenario, there are two * valid offsets for the local date-time. Calling this method will return * a zoned date-time with the earlier of the two selected. * <p> * If this method is called when it is not an overlap, {@code this} * is returned. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code ZonedDateTime} based on this date-time with the earlier offset, not null */ @Override public ZonedDateTime withEarlierOffsetAtOverlap() { ZoneOffsetTransition trans = getZone().getRules().getTransition(dateTime); if (trans != null && trans.isOverlap()) { ZoneOffset earlierOffset = trans.getOffsetBefore(); if (earlierOffset.equals(offset) == false) { return new ZonedDateTime(dateTime, earlierOffset, zone); } } return this; }
/** * Returns a copy of this date-time changing the zone offset to the * later of the two valid offsets at a local time-line overlap. * <p> * This method only has any effect when the local time-line overlaps, such as * at an autumn daylight savings cutover. In this scenario, there are two * valid offsets for the local date-time. Calling this method will return * a zoned date-time with the later of the two selected. * <p> * If this method is called when it is not an overlap, {@code this} * is returned. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code ZonedDateTime} based on this date-time with the later offset, not null */ @Override public ZonedDateTime withLaterOffsetAtOverlap() { ZoneOffsetTransition trans = getZone().getRules().getTransition(toLocalDateTime()); if (trans != null) { ZoneOffset laterOffset = trans.getOffsetAfter(); if (laterOffset.equals(offset) == false) { return new ZonedDateTime(dateTime, laterOffset, zone); } } return this; }
/** * Returns a copy of this date-time changing the zone offset to the * earlier of the two valid offsets at a local time-line overlap. * <p> * This method only has any effect when the local time-line overlaps, such as * at an autumn daylight savings cutover. In this scenario, there are two * valid offsets for the local date-time. Calling this method will return * a zoned date-time with the earlier of the two selected. * <p> * If this method is called when it is not an overlap, {@code this} * is returned. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code ZonedDateTime} based on this date-time with the earlier offset, not null */ @Override public ZonedDateTime withEarlierOffsetAtOverlap() { ZoneOffsetTransition trans = getZone().getRules().getTransition(dateTime); if (trans != null && trans.isOverlap()) { ZoneOffset earlierOffset = trans.getOffsetBefore(); if (earlierOffset.equals(offset) == false) { return new ZonedDateTime(dateTime, earlierOffset, zone); } } return this; }
/** * Returns a copy of this date-time changing the zone offset to the * later of the two valid offsets at a local time-line overlap. * <p> * This method only has any effect when the local time-line overlaps, such as * at an autumn daylight savings cutover. In this scenario, there are two * valid offsets for the local date-time. Calling this method will return * a zoned date-time with the later of the two selected. * <p> * If this method is called when it is not an overlap, {@code this} * is returned. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code ZonedDateTime} based on this date-time with the later offset, not null */ @Override public ZonedDateTime withLaterOffsetAtOverlap() { ZoneOffsetTransition trans = getZone().getRules().getTransition(toLocalDateTime()); if (trans != null) { ZoneOffset laterOffset = trans.getOffsetAfter(); if (laterOffset.equals(offset) == false) { return new ZonedDateTime(dateTime, laterOffset, zone); } } return this; }
localDateTime + "' in zone '" + zone + "'"); return new ZonedDateTime(localDateTime, offset, zone);
localDateTime + "' in zone '" + zone + "'"); return new ZonedDateTime(localDateTime, offset, zone);