src/share/classes/java/time/chrono/ChronoZonedDateTime.java

Print this page

        

*** 57,67 **** * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package java.time.temporal; import static java.time.temporal.ChronoField.INSTANT_SECONDS; import static java.time.temporal.ChronoField.OFFSET_SECONDS; import static java.time.temporal.ChronoUnit.NANOS; --- 57,67 ---- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ! package java.time.chrono; import static java.time.temporal.ChronoField.INSTANT_SECONDS; import static java.time.temporal.ChronoField.OFFSET_SECONDS; import static java.time.temporal.ChronoUnit.NANOS;
*** 70,79 **** --- 70,89 ---- import java.time.LocalTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; + import java.time.temporal.ChronoField; + import java.time.temporal.Queries; + import java.time.temporal.Temporal; + import java.time.temporal.TemporalAccessor; + import java.time.temporal.TemporalAdjuster; + import java.time.temporal.TemporalAmount; + import java.time.temporal.TemporalField; + import java.time.temporal.TemporalQuery; + import java.time.temporal.TemporalUnit; + import java.time.temporal.ValueRange; import java.util.Comparator; import java.util.Objects; /** * A date-time with a time-zone in an arbitrary chronology,
*** 81,91 **** * <p> * <b>Most applications should declare method signatures, fields and variables * as {@link ZonedDateTime}, not this interface.</b> * <p> * A {@code ChronoZonedDateTime} is the abstract representation of an offset date-time ! * where the {@code Chrono chronology}, or calendar system, is pluggable. * The date-time is defined in terms of fields expressed by {@link TemporalField}, * where most common implementations are defined in {@link ChronoField}. * The chronology defines how the calendar system operates and the meaning of * the standard fields. * --- 91,101 ---- * <p> * <b>Most applications should declare method signatures, fields and variables * as {@link ZonedDateTime}, not this interface.</b> * <p> * A {@code ChronoZonedDateTime} is the abstract representation of an offset date-time ! * where the {@code Chronology chronology}, or calendar system, is pluggable. * The date-time is defined in terms of fields expressed by {@link TemporalField}, * where most common implementations are defined in {@link ChronoField}. * The chronology defines how the calendar system operates and the meaning of * the standard fields. *
*** 100,113 **** * <h3>Specification for implementors</h3> * This interface must be implemented with care to ensure other classes operate correctly. * All implementations that can be instantiated must be final, immutable and thread-safe. * Subclasses should be Serializable wherever possible. * ! * @param <C> the chronology of this date-time * @since 1.8 */ ! public interface ChronoZonedDateTime<C extends Chrono<C>> extends Temporal, Comparable<ChronoZonedDateTime<?>> { /** * Comparator for two {@code ChronoZonedDateTime} instances ignoring the chronology. * <p> --- 110,123 ---- * <h3>Specification for implementors</h3> * This interface must be implemented with care to ensure other classes operate correctly. * All implementations that can be instantiated must be final, immutable and thread-safe. * Subclasses should be Serializable wherever possible. * ! * @param <D> the concrete type for the date of this date-time * @since 1.8 */ ! public interface ChronoZonedDateTime<D extends ChronoLocalDate<D>> extends Temporal, Comparable<ChronoZonedDateTime<?>> { /** * Comparator for two {@code ChronoZonedDateTime} instances ignoring the chronology. * <p>
*** 123,133 **** Comparator<ChronoZonedDateTime<?>> INSTANT_COMPARATOR = new Comparator<ChronoZonedDateTime<?>>() { @Override public int compare(ChronoZonedDateTime<?> datetime1, ChronoZonedDateTime<?> datetime2) { int cmp = Long.compare(datetime1.toEpochSecond(), datetime2.toEpochSecond()); if (cmp == 0) { ! cmp = Long.compare(datetime1.getTime().toNanoOfDay(), datetime2.getTime().toNanoOfDay()); } return cmp; } }; --- 133,143 ---- Comparator<ChronoZonedDateTime<?>> INSTANT_COMPARATOR = new Comparator<ChronoZonedDateTime<?>>() { @Override public int compare(ChronoZonedDateTime<?> datetime1, ChronoZonedDateTime<?> datetime2) { int cmp = Long.compare(datetime1.toEpochSecond(), datetime2.toEpochSecond()); if (cmp == 0) { ! cmp = Long.compare(datetime1.toLocalTime().toNanoOfDay(), datetime2.toLocalTime().toNanoOfDay()); } return cmp; } };
*** 135,157 **** public default ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) { return field.range(); } ! return getDateTime().range(field); } ! return field.doRange(this); } @Override public default int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new DateTimeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } ! return getDateTime().get(field); } return Temporal.super.get(field); } @Override --- 145,167 ---- public default ValueRange range(TemporalField field) { if (field instanceof ChronoField) { if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) { return field.range(); } ! return toLocalDateTime().range(field); } ! return field.rangeRefinedBy(this); } @Override public default int get(TemporalField field) { if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: throw new DateTimeException("Field too large for an int: " + field); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } ! return toLocalDateTime().get(field); } return Temporal.super.get(field); } @Override
*** 159,206 **** if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } ! return getDateTime().getLong(field); } ! return field.doGet(this); } /** * Gets the local date part of this date-time. * <p> * This returns a local date with the same year, month and day * as this date-time. * * @return the date part of this date-time, not null */ ! public default ChronoLocalDate<C> getDate() { ! return getDateTime().getDate(); } /** * Gets the local time part of this date-time. * <p> * This returns a local time with the same hour, minute, second and * nanosecond as this date-time. * * @return the time part of this date-time, not null */ ! public default LocalTime getTime() { ! return getDateTime().getTime(); } /** * Gets the local date-time part of this date-time. * <p> * This returns a local date with the same year, month and day * as this date-time. * * @return the local date-time part of this date-time, not null */ ! ChronoLocalDateTime<C> getDateTime(); /** * Gets the zone offset, such as '+01:00'. * <p> * This is the offset of the local date-time from UTC/Greenwich. --- 169,216 ---- if (field instanceof ChronoField) { switch ((ChronoField) field) { case INSTANT_SECONDS: return toEpochSecond(); case OFFSET_SECONDS: return getOffset().getTotalSeconds(); } ! return toLocalDateTime().getLong(field); } ! return field.getFrom(this); } /** * Gets the local date part of this date-time. * <p> * This returns a local date with the same year, month and day * as this date-time. * * @return the date part of this date-time, not null */ ! public default D toLocalDate() { ! return toLocalDateTime().toLocalDate(); } /** * Gets the local time part of this date-time. * <p> * This returns a local time with the same hour, minute, second and * nanosecond as this date-time. * * @return the time part of this date-time, not null */ ! public default LocalTime toLocalTime() { ! return toLocalDateTime().toLocalTime(); } /** * Gets the local date-time part of this date-time. * <p> * This returns a local date with the same year, month and day * as this date-time. * * @return the local date-time part of this date-time, not null */ ! ChronoLocalDateTime<D> toLocalDateTime(); /** * Gets the zone offset, such as '+01:00'. * <p> * This is the offset of the local date-time from UTC/Greenwich.
*** 235,245 **** * * @return a {@code ZoneChronoDateTime} based on this date-time with the earlier offset, not null * @throws DateTimeException if no rules can be found for the zone * @throws DateTimeException if no rules are valid for this date-time */ ! ChronoZonedDateTime<C> withEarlierOffsetAtOverlap(); /** * 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> --- 245,255 ---- * * @return a {@code ZoneChronoDateTime} based on this date-time with the earlier offset, not null * @throws DateTimeException if no rules can be found for the zone * @throws DateTimeException if no rules are valid for this date-time */ ! ChronoZonedDateTime<D> withEarlierOffsetAtOverlap(); /** * 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>
*** 255,265 **** * * @return a {@code ChronoZonedDateTime} based on this date-time with the later offset, not null * @throws DateTimeException if no rules can be found for the zone * @throws DateTimeException if no rules are valid for this date-time */ ! ChronoZonedDateTime<C> withLaterOffsetAtOverlap(); /** * Returns a copy of this ZonedDateTime with a different time-zone, * retaining the local date-time if possible. * <p> --- 265,275 ---- * * @return a {@code ChronoZonedDateTime} based on this date-time with the later offset, not null * @throws DateTimeException if no rules can be found for the zone * @throws DateTimeException if no rules are valid for this date-time */ ! ChronoZonedDateTime<D> withLaterOffsetAtOverlap(); /** * Returns a copy of this ZonedDateTime with a different time-zone, * retaining the local date-time if possible. * <p>
*** 272,282 **** * This instance is immutable and unaffected by this method call. * * @param zone the time-zone to change to, not null * @return a {@code ChronoZonedDateTime} based on this date-time with the requested zone, not null */ ! ChronoZonedDateTime<C> withZoneSameLocal(ZoneId zone); /** * Returns a copy of this date-time with a different time-zone, * retaining the instant. * <p> --- 282,292 ---- * This instance is immutable and unaffected by this method call. * * @param zone the time-zone to change to, not null * @return a {@code ChronoZonedDateTime} based on this date-time with the requested zone, not null */ ! ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone); /** * Returns a copy of this date-time with a different time-zone, * retaining the instant. * <p>
*** 291,358 **** * * @param zone the time-zone to change to, not null * @return a {@code ChronoZonedDateTime} based on this date-time with the requested zone, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoZonedDateTime<C> withZoneSameInstant(ZoneId zone); //----------------------------------------------------------------------- // override for covariant return type /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<C> with(TemporalAdjuster adjuster) { ! return getDate().getChrono().ensureChronoZonedDateTime(Temporal.super.with(adjuster)); } /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! ChronoZonedDateTime<C> with(TemporalField field, long newValue); /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<C> plus(TemporalAdder adder) { ! return getDate().getChrono().ensureChronoZonedDateTime(Temporal.super.plus(adder)); } /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! ChronoZonedDateTime<C> plus(long amountToAdd, TemporalUnit unit); /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<C> minus(TemporalSubtractor subtractor) { ! return getDate().getChrono().ensureChronoZonedDateTime(Temporal.super.minus(subtractor)); } /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<C> minus(long amountToSubtract, TemporalUnit unit) { ! return getDate().getChrono().ensureChronoZonedDateTime(Temporal.super.minus(amountToSubtract, unit)); } //----------------------------------------------------------------------- /** * Queries this date-time using the specified query. --- 301,371 ---- * * @param zone the time-zone to change to, not null * @return a {@code ChronoZonedDateTime} based on this date-time with the requested zone, not null * @throws DateTimeException if the result exceeds the supported date range */ ! ChronoZonedDateTime<D> withZoneSameInstant(ZoneId zone); ! ! @Override // Override to provide javadoc ! public boolean isSupported(TemporalField field); //----------------------------------------------------------------------- // override for covariant return type /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<D> with(TemporalAdjuster adjuster) { ! return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(Temporal.super.with(adjuster))); } /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! ChronoZonedDateTime<D> with(TemporalField field, long newValue); /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<D> plus(TemporalAmount amount) { ! return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(Temporal.super.plus(amount))); } /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit); /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<D> minus(TemporalAmount amount) { ! return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(Temporal.super.minus(amount))); } /** * {@inheritDoc} * @throws DateTimeException {@inheritDoc} * @throws ArithmeticException {@inheritDoc} */ @Override ! public default ChronoZonedDateTime<D> minus(long amountToSubtract, TemporalUnit unit) { ! return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(Temporal.super.minus(amountToSubtract, unit))); } //----------------------------------------------------------------------- /** * Queries this date-time using the specified query.
*** 375,422 **** @SuppressWarnings("unchecked") @Override public default <R> R query(TemporalQuery<R> query) { if (query == Queries.zone() || query == Queries.zoneId()) { return (R) getZone(); - } else if (query == Queries.chrono()) { - return (R) getDate().getChrono(); - } else if (query == Queries.precision()) { - return (R) NANOS; } else if (query == Queries.offset()) { return (R) getOffset(); } // inline TemporalAccessor.super.query(query) as an optimization return query.queryFrom(this); } //----------------------------------------------------------------------- /** * Converts this date-time to an {@code Instant}. * <p> ! * This combines the {@linkplain #getDateTime() local date-time} and ! * {@linkplain #getOffset() offset} to form an {@code Instant}. * * @return an {@code Instant} representing the same instant, not null */ public default Instant toInstant() { ! return Instant.ofEpochSecond(toEpochSecond(), getTime().getNano()); } /** * Converts this date-time to the number of seconds from the epoch * of 1970-01-01T00:00:00Z. * <p> ! * This uses the {@linkplain #getDateTime() local date-time} and * {@linkplain #getOffset() offset} to calculate the epoch-second value, * which is the number of elapsed seconds from 1970-01-01T00:00:00Z. * Instants on the time-line after the epoch are positive, earlier are negative. * * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z */ public default long toEpochSecond() { ! long epochDay = getDate().toEpochDay(); ! long secs = epochDay * 86400 + getTime().toSecondOfDay(); secs -= getOffset().getTotalSeconds(); return secs; } //----------------------------------------------------------------------- --- 388,440 ---- @SuppressWarnings("unchecked") @Override public default <R> R query(TemporalQuery<R> query) { if (query == Queries.zone() || query == Queries.zoneId()) { return (R) getZone(); } else if (query == Queries.offset()) { return (R) getOffset(); + } else if (query == Queries.localTime()) { + return (R) toLocalTime(); + } else if (query == Queries.chronology()) { + return (R) toLocalDate().getChronology(); + } else if (query == Queries.precision()) { + return (R) NANOS; } // inline TemporalAccessor.super.query(query) as an optimization + // non-JDK classes are not permitted to make this optimization return query.queryFrom(this); } //----------------------------------------------------------------------- /** * Converts this date-time to an {@code Instant}. * <p> ! * This returns an {@code Instant} representing the same point on the ! * time-line as this date-time. The calculation combines the ! * {@linkplain #toLocalDateTime() local date-time} and ! * {@linkplain #getOffset() offset}. * * @return an {@code Instant} representing the same instant, not null */ public default Instant toInstant() { ! return Instant.ofEpochSecond(toEpochSecond(), toLocalTime().getNano()); } /** * Converts this date-time to the number of seconds from the epoch * of 1970-01-01T00:00:00Z. * <p> ! * This uses the {@linkplain #toLocalDateTime() local date-time} and * {@linkplain #getOffset() offset} to calculate the epoch-second value, * which is the number of elapsed seconds from 1970-01-01T00:00:00Z. * Instants on the time-line after the epoch are positive, earlier are negative. * * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z */ public default long toEpochSecond() { ! long epochDay = toLocalDate().toEpochDay(); ! long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); secs -= getOffset().getTotalSeconds(); return secs; } //-----------------------------------------------------------------------
*** 437,453 **** */ @Override public default int compareTo(ChronoZonedDateTime<?> other) { int cmp = Long.compare(toEpochSecond(), other.toEpochSecond()); if (cmp == 0) { ! cmp = getTime().getNano() - other.getTime().getNano(); if (cmp == 0) { ! cmp = getDateTime().compareTo(other.getDateTime()); if (cmp == 0) { cmp = getZone().getId().compareTo(other.getZone().getId()); if (cmp == 0) { ! cmp = getDate().getChrono().compareTo(other.getDate().getChrono()); } } } } return cmp; --- 455,471 ---- */ @Override public default int compareTo(ChronoZonedDateTime<?> other) { int cmp = Long.compare(toEpochSecond(), other.toEpochSecond()); if (cmp == 0) { ! cmp = toLocalTime().getNano() - other.toLocalTime().getNano(); if (cmp == 0) { ! cmp = toLocalDateTime().compareTo(other.toLocalDateTime()); if (cmp == 0) { cmp = getZone().getId().compareTo(other.getZone().getId()); if (cmp == 0) { ! cmp = toLocalDate().getChronology().compareTo(other.toLocalDate().getChronology()); } } } } return cmp;
*** 468,478 **** */ public default boolean isBefore(ChronoZonedDateTime<?> other) { long thisEpochSec = toEpochSecond(); long otherEpochSec = other.toEpochSecond(); return thisEpochSec < otherEpochSec || ! (thisEpochSec == otherEpochSec && getTime().getNano() < other.getTime().getNano()); } /** * Checks if the instant of this date-time is after that of the specified date-time. * <p> --- 486,496 ---- */ public default boolean isBefore(ChronoZonedDateTime<?> other) { long thisEpochSec = toEpochSecond(); long otherEpochSec = other.toEpochSecond(); return thisEpochSec < otherEpochSec || ! (thisEpochSec == otherEpochSec && toLocalTime().getNano() < other.toLocalTime().getNano()); } /** * Checks if the instant of this date-time is after that of the specified date-time. * <p>
*** 488,498 **** */ public default boolean isAfter(ChronoZonedDateTime<?> other) { long thisEpochSec = toEpochSecond(); long otherEpochSec = other.toEpochSecond(); return thisEpochSec > otherEpochSec || ! (thisEpochSec == otherEpochSec && getTime().getNano() > other.getTime().getNano()); } /** * Checks if the instant of this date-time is equal to that of the specified date-time. * <p> --- 506,516 ---- */ public default boolean isAfter(ChronoZonedDateTime<?> other) { long thisEpochSec = toEpochSecond(); long otherEpochSec = other.toEpochSecond(); return thisEpochSec > otherEpochSec || ! (thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano()); } /** * Checks if the instant of this date-time is equal to that of the specified date-time. * <p>
*** 506,516 **** * @param other the other date-time to compare to, not null * @return true if the instant equals the instant of the specified date-time */ public default boolean isEqual(ChronoZonedDateTime<?> other) { return toEpochSecond() == other.toEpochSecond() && ! getTime().getNano() == other.getTime().getNano(); } //----------------------------------------------------------------------- /** * Checks if this date-time is equal to another date-time. --- 524,534 ---- * @param other the other date-time to compare to, not null * @return true if the instant equals the instant of the specified date-time */ public default boolean isEqual(ChronoZonedDateTime<?> other) { return toEpochSecond() == other.toEpochSecond() && ! toLocalTime().getNano() == other.toLocalTime().getNano(); } //----------------------------------------------------------------------- /** * Checks if this date-time is equal to another date-time.
*** 547,564 **** /** * Outputs this date-time as a {@code String} using the formatter. * <p> * The default implementation must behave as follows: * <pre> ! * return formatter.print(this); * </pre> * * @param formatter the formatter to use, not null * @return the formatted date-time string, not null * @throws DateTimeException if an error occurs during printing */ public default String toString(DateTimeFormatter formatter) { Objects.requireNonNull(formatter, "formatter"); ! return formatter.print(this); } } --- 565,582 ---- /** * Outputs this date-time as a {@code String} using the formatter. * <p> * The default implementation must behave as follows: * <pre> ! * return formatter.format(this); * </pre> * * @param formatter the formatter to use, not null * @return the formatted date-time string, not null * @throws DateTimeException if an error occurs during printing */ public default String toString(DateTimeFormatter formatter) { Objects.requireNonNull(formatter, "formatter"); ! return formatter.format(this); } }