src/share/classes/java/time/chrono/ChronoZonedDateTime.java
Print this page
@@ -57,11 +57,11 @@
* 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;
+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,10 +70,20 @@
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,11 +91,11 @@
* <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.
+ * 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,14 +110,14 @@
* <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
+ * @param <D> the concrete type for the date of this date-time
* @since 1.8
*/
-public interface ChronoZonedDateTime<C extends Chrono<C>>
+public interface ChronoZonedDateTime<D extends ChronoLocalDate<D>>
extends Temporal, Comparable<ChronoZonedDateTime<?>> {
/**
* Comparator for two {@code ChronoZonedDateTime} instances ignoring the chronology.
* <p>
@@ -123,11 +133,11 @@
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());
+ cmp = Long.compare(datetime1.toLocalTime().toNanoOfDay(), datetime2.toLocalTime().toNanoOfDay());
}
return cmp;
}
};
@@ -135,23 +145,23 @@
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 toLocalDateTime().range(field);
}
- return field.doRange(this);
+ 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 getDateTime().get(field);
+ return toLocalDateTime().get(field);
}
return Temporal.super.get(field);
}
@Override
@@ -159,48 +169,48 @@
if (field instanceof ChronoField) {
switch ((ChronoField) field) {
case INSTANT_SECONDS: return toEpochSecond();
case OFFSET_SECONDS: return getOffset().getTotalSeconds();
}
- return getDateTime().getLong(field);
+ return toLocalDateTime().getLong(field);
}
- return field.doGet(this);
+ 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 ChronoLocalDate<C> getDate() {
- return getDateTime().getDate();
+ 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 getTime() {
- return getDateTime().getTime();
+ 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<C> getDateTime();
+ 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,11 +245,11 @@
*
* @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();
+ 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,11 +265,11 @@
*
* @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();
+ ChronoZonedDateTime<D> withLaterOffsetAtOverlap();
/**
* Returns a copy of this ZonedDateTime with a different time-zone,
* retaining the local date-time if possible.
* <p>
@@ -272,11 +282,11 @@
* 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);
+ ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone);
/**
* Returns a copy of this date-time with a different time-zone,
* retaining the instant.
* <p>
@@ -291,68 +301,71 @@
*
* @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);
+ 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<C> with(TemporalAdjuster adjuster) {
- return getDate().getChrono().ensureChronoZonedDateTime(Temporal.super.with(adjuster));
+ 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<C> with(TemporalField field, long newValue);
+ ChronoZonedDateTime<D> 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));
+ 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<C> plus(long amountToAdd, TemporalUnit unit);
+ ChronoZonedDateTime<D> 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));
+ 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<C> minus(long amountToSubtract, TemporalUnit unit) {
- return getDate().getChrono().ensureChronoZonedDateTime(Temporal.super.minus(amountToSubtract, unit));
+ 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,48 +388,53 @@
@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();
+ } 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 combines the {@linkplain #getDateTime() local date-time} and
- * {@linkplain #getOffset() offset} to form an {@code Instant}.
+ * 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(), getTime().getNano());
+ 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 #getDateTime() local date-time} and
+ * 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 = getDate().toEpochDay();
- long secs = epochDay * 86400 + getTime().toSecondOfDay();
+ long epochDay = toLocalDate().toEpochDay();
+ long secs = epochDay * 86400 + toLocalTime().toSecondOfDay();
secs -= getOffset().getTotalSeconds();
return secs;
}
//-----------------------------------------------------------------------
@@ -437,17 +455,17 @@
*/
@Override
public default int compareTo(ChronoZonedDateTime<?> other) {
int cmp = Long.compare(toEpochSecond(), other.toEpochSecond());
if (cmp == 0) {
- cmp = getTime().getNano() - other.getTime().getNano();
+ cmp = toLocalTime().getNano() - other.toLocalTime().getNano();
if (cmp == 0) {
- cmp = getDateTime().compareTo(other.getDateTime());
+ cmp = toLocalDateTime().compareTo(other.toLocalDateTime());
if (cmp == 0) {
cmp = getZone().getId().compareTo(other.getZone().getId());
if (cmp == 0) {
- cmp = getDate().getChrono().compareTo(other.getDate().getChrono());
+ cmp = toLocalDate().getChronology().compareTo(other.toLocalDate().getChronology());
}
}
}
}
return cmp;
@@ -468,11 +486,11 @@
*/
public default boolean isBefore(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec < otherEpochSec ||
- (thisEpochSec == otherEpochSec && getTime().getNano() < other.getTime().getNano());
+ (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,11 +506,11 @@
*/
public default boolean isAfter(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec > otherEpochSec ||
- (thisEpochSec == otherEpochSec && getTime().getNano() > other.getTime().getNano());
+ (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,11 +524,11 @@
* @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();
+ toLocalTime().getNano() == other.toLocalTime().getNano();
}
//-----------------------------------------------------------------------
/**
* Checks if this date-time is equal to another date-time.
@@ -547,18 +565,18 @@
/**
* Outputs this date-time as a {@code String} using the formatter.
* <p>
* The default implementation must behave as follows:
* <pre>
- * return formatter.print(this);
+ * 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.print(this);
+ return formatter.format(this);
}
}