1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * This file is available under and governed by the GNU General Public 28 * License version 2 only, as published by the Free Software Foundation. 29 * However, the following notice accompanied the original version of this 30 * file: 31 * 32 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos 33 * 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions are met: 38 * 39 * * Redistributions of source code must retain the above copyright notice, 40 * this list of conditions and the following disclaimer. 41 * 42 * * Redistributions in binary form must reproduce the above copyright notice, 43 * this list of conditions and the following disclaimer in the documentation 44 * and/or other materials provided with the distribution. 45 * 46 * * Neither the name of JSR-310 nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 54 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 55 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 57 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 58 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 59 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 package java.time.chrono; 63 64 import static java.time.temporal.ChronoField.INSTANT_SECONDS; 65 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 66 import static java.time.temporal.ChronoUnit.FOREVER; 67 import static java.time.temporal.ChronoUnit.NANOS; 68 69 import java.time.DateTimeException; 70 import java.time.Instant; 71 import java.time.LocalTime; 72 import java.time.ZoneId; 73 import java.time.ZoneOffset; 74 import java.time.ZonedDateTime; 75 import java.time.format.DateTimeFormatter; 76 import java.time.temporal.ChronoField; 77 import java.time.temporal.ChronoUnit; 78 import java.time.temporal.Temporal; 79 import java.time.temporal.TemporalAccessor; 80 import java.time.temporal.TemporalAdjuster; 81 import java.time.temporal.TemporalAmount; 82 import java.time.temporal.TemporalField; 83 import java.time.temporal.TemporalQueries; 84 import java.time.temporal.TemporalQuery; 85 import java.time.temporal.TemporalUnit; 86 import java.time.temporal.UnsupportedTemporalTypeException; 87 import java.time.temporal.ValueRange; 88 import java.util.Comparator; 89 import java.util.Objects; 90 91 /** 92 * A date-time with a time-zone in an arbitrary chronology, 93 * intended for advanced globalization use cases. 94 * <p> 95 * <b>Most applications should declare method signatures, fields and variables 96 * as {@link ZonedDateTime}, not this interface.</b> 97 * <p> 98 * A {@code ChronoZonedDateTime} is the abstract representation of an offset date-time 99 * where the {@code Chronology chronology}, or calendar system, is pluggable. 100 * The date-time is defined in terms of fields expressed by {@link TemporalField}, 101 * where most common implementations are defined in {@link ChronoField}. 102 * The chronology defines how the calendar system operates and the meaning of 103 * the standard fields. 104 * 105 * <h3>When to use this interface</h3> 106 * The design of the API encourages the use of {@code ZonedDateTime} rather than this 107 * interface, even in the case where the application needs to deal with multiple 108 * calendar systems. The rationale for this is explored in detail in {@link ChronoLocalDate}. 109 * <p> 110 * Ensure that the discussion in {@code ChronoLocalDate} has been read and understood 111 * before using this interface. 112 * 113 * @implSpec 114 * This interface must be implemented with care to ensure other classes operate correctly. 115 * All implementations that can be instantiated must be final, immutable and thread-safe. 116 * Subclasses should be Serializable wherever possible. 117 * 118 * @param <D> the concrete type for the date of this date-time 119 * @since 1.8 120 */ 121 public interface ChronoZonedDateTime<D extends ChronoLocalDate> 122 extends Temporal, Comparable<ChronoZonedDateTime<?>> { 123 124 /** 125 * Gets a comparator that compares {@code ChronoZonedDateTime} in 126 * time-line order ignoring the chronology. 127 * <p> 128 * This comparator differs from the comparison in {@link #compareTo} in that it 129 * only compares the underlying instant and not the chronology. 130 * This allows dates in different calendar systems to be compared based 131 * on the position of the date-time on the instant time-line. 132 * The underlying comparison is equivalent to comparing the epoch-second and nano-of-second. 133 * 134 * @return a comparator that compares in time-line order ignoring the chronology 135 * @see #isAfter 136 * @see #isBefore 137 * @see #isEqual 138 */ timeLineOrder()139 static Comparator<ChronoZonedDateTime<?>> timeLineOrder() { 140 return AbstractChronology.INSTANT_ORDER; 141 } 142 143 //----------------------------------------------------------------------- 144 /** 145 * Obtains an instance of {@code ChronoZonedDateTime} from a temporal object. 146 * <p> 147 * This creates a zoned date-time based on the specified temporal. 148 * A {@code TemporalAccessor} represents an arbitrary set of date and time information, 149 * which this factory converts to an instance of {@code ChronoZonedDateTime}. 150 * <p> 151 * The conversion extracts and combines the chronology, date, time and zone 152 * from the temporal object. The behavior is equivalent to using 153 * {@link Chronology#zonedDateTime(TemporalAccessor)} with the extracted chronology. 154 * Implementations are permitted to perform optimizations such as accessing 155 * those fields that are equivalent to the relevant objects. 156 * <p> 157 * This method matches the signature of the functional interface {@link TemporalQuery} 158 * allowing it to be used as a query via method reference, {@code ChronoZonedDateTime::from}. 159 * 160 * @param temporal the temporal object to convert, not null 161 * @return the date-time, not null 162 * @throws DateTimeException if unable to convert to a {@code ChronoZonedDateTime} 163 * @see Chronology#zonedDateTime(TemporalAccessor) 164 */ from(TemporalAccessor temporal)165 static ChronoZonedDateTime<?> from(TemporalAccessor temporal) { 166 if (temporal instanceof ChronoZonedDateTime) { 167 return (ChronoZonedDateTime<?>) temporal; 168 } 169 Objects.requireNonNull(temporal, "temporal"); 170 Chronology chrono = temporal.query(TemporalQueries.chronology()); 171 if (chrono == null) { 172 throw new DateTimeException("Unable to obtain ChronoZonedDateTime from TemporalAccessor: " + temporal.getClass()); 173 } 174 return chrono.zonedDateTime(temporal); 175 } 176 177 //----------------------------------------------------------------------- 178 @Override range(TemporalField field)179 default ValueRange range(TemporalField field) { 180 if (field instanceof ChronoField) { 181 if (field == INSTANT_SECONDS || field == OFFSET_SECONDS) { 182 return field.range(); 183 } 184 return toLocalDateTime().range(field); 185 } 186 return field.rangeRefinedBy(this); 187 } 188 189 @Override get(TemporalField field)190 default int get(TemporalField field) { 191 if (field instanceof ChronoField) { 192 switch ((ChronoField) field) { 193 case INSTANT_SECONDS: 194 throw new UnsupportedTemporalTypeException("Invalid field 'InstantSeconds' for get() method, use getLong() instead"); 195 case OFFSET_SECONDS: 196 return getOffset().getTotalSeconds(); 197 } 198 return toLocalDateTime().get(field); 199 } 200 return Temporal.super.get(field); 201 } 202 203 @Override getLong(TemporalField field)204 default long getLong(TemporalField field) { 205 if (field instanceof ChronoField) { 206 switch ((ChronoField) field) { 207 case INSTANT_SECONDS: return toEpochSecond(); 208 case OFFSET_SECONDS: return getOffset().getTotalSeconds(); 209 } 210 return toLocalDateTime().getLong(field); 211 } 212 return field.getFrom(this); 213 } 214 215 /** 216 * Gets the local date part of this date-time. 217 * <p> 218 * This returns a local date with the same year, month and day 219 * as this date-time. 220 * 221 * @return the date part of this date-time, not null 222 */ toLocalDate()223 default D toLocalDate() { 224 return toLocalDateTime().toLocalDate(); 225 } 226 227 /** 228 * Gets the local time part of this date-time. 229 * <p> 230 * This returns a local time with the same hour, minute, second and 231 * nanosecond as this date-time. 232 * 233 * @return the time part of this date-time, not null 234 */ toLocalTime()235 default LocalTime toLocalTime() { 236 return toLocalDateTime().toLocalTime(); 237 } 238 239 /** 240 * Gets the local date-time part of this date-time. 241 * <p> 242 * This returns a local date with the same year, month and day 243 * as this date-time. 244 * 245 * @return the local date-time part of this date-time, not null 246 */ toLocalDateTime()247 ChronoLocalDateTime<D> toLocalDateTime(); 248 249 /** 250 * Gets the chronology of this date-time. 251 * <p> 252 * The {@code Chronology} represents the calendar system in use. 253 * The era and other fields in {@link ChronoField} are defined by the chronology. 254 * 255 * @return the chronology, not null 256 */ getChronology()257 default Chronology getChronology() { 258 return toLocalDate().getChronology(); 259 } 260 261 /** 262 * Gets the zone offset, such as '+01:00'. 263 * <p> 264 * This is the offset of the local date-time from UTC/Greenwich. 265 * 266 * @return the zone offset, not null 267 */ getOffset()268 ZoneOffset getOffset(); 269 270 /** 271 * Gets the zone ID, such as 'Europe/Paris'. 272 * <p> 273 * This returns the stored time-zone id used to determine the time-zone rules. 274 * 275 * @return the zone ID, not null 276 */ getZone()277 ZoneId getZone(); 278 279 //----------------------------------------------------------------------- 280 /** 281 * Returns a copy of this date-time changing the zone offset to the 282 * earlier of the two valid offsets at a local time-line overlap. 283 * <p> 284 * This method only has any effect when the local time-line overlaps, such as 285 * at an autumn daylight savings cutover. In this scenario, there are two 286 * valid offsets for the local date-time. Calling this method will return 287 * a zoned date-time with the earlier of the two selected. 288 * <p> 289 * If this method is called when it is not an overlap, {@code this} 290 * is returned. 291 * <p> 292 * This instance is immutable and unaffected by this method call. 293 * 294 * @return a {@code ChronoZonedDateTime} based on this date-time with the earlier offset, not null 295 * @throws DateTimeException if no rules can be found for the zone 296 * @throws DateTimeException if no rules are valid for this date-time 297 */ withEarlierOffsetAtOverlap()298 ChronoZonedDateTime<D> withEarlierOffsetAtOverlap(); 299 300 /** 301 * Returns a copy of this date-time changing the zone offset to the 302 * later of the two valid offsets at a local time-line overlap. 303 * <p> 304 * This method only has any effect when the local time-line overlaps, such as 305 * at an autumn daylight savings cutover. In this scenario, there are two 306 * valid offsets for the local date-time. Calling this method will return 307 * a zoned date-time with the later of the two selected. 308 * <p> 309 * If this method is called when it is not an overlap, {@code this} 310 * is returned. 311 * <p> 312 * This instance is immutable and unaffected by this method call. 313 * 314 * @return a {@code ChronoZonedDateTime} based on this date-time with the later offset, not null 315 * @throws DateTimeException if no rules can be found for the zone 316 * @throws DateTimeException if no rules are valid for this date-time 317 */ withLaterOffsetAtOverlap()318 ChronoZonedDateTime<D> withLaterOffsetAtOverlap(); 319 320 /** 321 * Returns a copy of this date-time with a different time-zone, 322 * retaining the local date-time if possible. 323 * <p> 324 * This method changes the time-zone and retains the local date-time. 325 * The local date-time is only changed if it is invalid for the new zone. 326 * <p> 327 * To change the zone and adjust the local date-time, 328 * use {@link #withZoneSameInstant(ZoneId)}. 329 * <p> 330 * This instance is immutable and unaffected by this method call. 331 * 332 * @param zone the time-zone to change to, not null 333 * @return a {@code ChronoZonedDateTime} based on this date-time with the requested zone, not null 334 */ withZoneSameLocal(ZoneId zone)335 ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone); 336 337 /** 338 * Returns a copy of this date-time with a different time-zone, 339 * retaining the instant. 340 * <p> 341 * This method changes the time-zone and retains the instant. 342 * This normally results in a change to the local date-time. 343 * <p> 344 * This method is based on retaining the same instant, thus gaps and overlaps 345 * in the local time-line have no effect on the result. 346 * <p> 347 * To change the offset while keeping the local time, 348 * use {@link #withZoneSameLocal(ZoneId)}. 349 * 350 * @param zone the time-zone to change to, not null 351 * @return a {@code ChronoZonedDateTime} based on this date-time with the requested zone, not null 352 * @throws DateTimeException if the result exceeds the supported date range 353 */ withZoneSameInstant(ZoneId zone)354 ChronoZonedDateTime<D> withZoneSameInstant(ZoneId zone); 355 356 /** 357 * Checks if the specified field is supported. 358 * <p> 359 * This checks if the specified field can be queried on this date-time. 360 * If false, then calling the {@link #range(TemporalField) range}, 361 * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)} 362 * methods will throw an exception. 363 * <p> 364 * The set of supported fields is defined by the chronology and normally includes 365 * all {@code ChronoField} fields. 366 * <p> 367 * If the field is not a {@code ChronoField}, then the result of this method 368 * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)} 369 * passing {@code this} as the argument. 370 * Whether the field is supported is determined by the field. 371 * 372 * @param field the field to check, null returns false 373 * @return true if the field can be queried, false if not 374 */ 375 @Override isSupported(TemporalField field)376 boolean isSupported(TemporalField field); 377 378 /** 379 * Checks if the specified unit is supported. 380 * <p> 381 * This checks if the specified unit can be added to or subtracted from this date-time. 382 * If false, then calling the {@link #plus(long, TemporalUnit)} and 383 * {@link #minus(long, TemporalUnit) minus} methods will throw an exception. 384 * <p> 385 * The set of supported units is defined by the chronology and normally includes 386 * all {@code ChronoUnit} units except {@code FOREVER}. 387 * <p> 388 * If the unit is not a {@code ChronoUnit}, then the result of this method 389 * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)} 390 * passing {@code this} as the argument. 391 * Whether the unit is supported is determined by the unit. 392 * 393 * @param unit the unit to check, null returns false 394 * @return true if the unit can be added/subtracted, false if not 395 */ 396 @Override isSupported(TemporalUnit unit)397 default boolean isSupported(TemporalUnit unit) { 398 if (unit instanceof ChronoUnit) { 399 return unit != FOREVER; 400 } 401 return unit != null && unit.isSupportedBy(this); 402 } 403 404 //----------------------------------------------------------------------- 405 // override for covariant return type 406 /** 407 * {@inheritDoc} 408 * @throws DateTimeException {@inheritDoc} 409 * @throws ArithmeticException {@inheritDoc} 410 */ 411 @Override with(TemporalAdjuster adjuster)412 default ChronoZonedDateTime<D> with(TemporalAdjuster adjuster) { 413 return ChronoZonedDateTimeImpl.ensureValid(getChronology(), Temporal.super.with(adjuster)); 414 } 415 416 /** 417 * {@inheritDoc} 418 * @throws DateTimeException {@inheritDoc} 419 * @throws ArithmeticException {@inheritDoc} 420 */ 421 @Override with(TemporalField field, long newValue)422 ChronoZonedDateTime<D> with(TemporalField field, long newValue); 423 424 /** 425 * {@inheritDoc} 426 * @throws DateTimeException {@inheritDoc} 427 * @throws ArithmeticException {@inheritDoc} 428 */ 429 @Override plus(TemporalAmount amount)430 default ChronoZonedDateTime<D> plus(TemporalAmount amount) { 431 return ChronoZonedDateTimeImpl.ensureValid(getChronology(), Temporal.super.plus(amount)); 432 } 433 434 /** 435 * {@inheritDoc} 436 * @throws DateTimeException {@inheritDoc} 437 * @throws ArithmeticException {@inheritDoc} 438 */ 439 @Override plus(long amountToAdd, TemporalUnit unit)440 ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit); 441 442 /** 443 * {@inheritDoc} 444 * @throws DateTimeException {@inheritDoc} 445 * @throws ArithmeticException {@inheritDoc} 446 */ 447 @Override minus(TemporalAmount amount)448 default ChronoZonedDateTime<D> minus(TemporalAmount amount) { 449 return ChronoZonedDateTimeImpl.ensureValid(getChronology(), Temporal.super.minus(amount)); 450 } 451 452 /** 453 * {@inheritDoc} 454 * @throws DateTimeException {@inheritDoc} 455 * @throws ArithmeticException {@inheritDoc} 456 */ 457 @Override minus(long amountToSubtract, TemporalUnit unit)458 default ChronoZonedDateTime<D> minus(long amountToSubtract, TemporalUnit unit) { 459 return ChronoZonedDateTimeImpl.ensureValid(getChronology(), Temporal.super.minus(amountToSubtract, unit)); 460 } 461 462 //----------------------------------------------------------------------- 463 /** 464 * Queries this date-time using the specified query. 465 * <p> 466 * This queries this date-time using the specified query strategy object. 467 * The {@code TemporalQuery} object defines the logic to be used to 468 * obtain the result. Read the documentation of the query to understand 469 * what the result of this method will be. 470 * <p> 471 * The result of this method is obtained by invoking the 472 * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the 473 * specified query passing {@code this} as the argument. 474 * 475 * @param <R> the type of the result 476 * @param query the query to invoke, not null 477 * @return the query result, null may be returned (defined by the query) 478 * @throws DateTimeException if unable to query (defined by the query) 479 * @throws ArithmeticException if numeric overflow occurs (defined by the query) 480 */ 481 @SuppressWarnings("unchecked") 482 @Override query(TemporalQuery<R> query)483 default <R> R query(TemporalQuery<R> query) { 484 if (query == TemporalQueries.zone() || query == TemporalQueries.zoneId()) { 485 return (R) getZone(); 486 } else if (query == TemporalQueries.offset()) { 487 return (R) getOffset(); 488 } else if (query == TemporalQueries.localTime()) { 489 return (R) toLocalTime(); 490 } else if (query == TemporalQueries.chronology()) { 491 return (R) getChronology(); 492 } else if (query == TemporalQueries.precision()) { 493 return (R) NANOS; 494 } 495 // inline TemporalAccessor.super.query(query) as an optimization 496 // non-JDK classes are not permitted to make this optimization 497 return query.queryFrom(this); 498 } 499 500 /** 501 * Formats this date-time using the specified formatter. 502 * <p> 503 * This date-time will be passed to the formatter to produce a string. 504 * <p> 505 * The default implementation must behave as follows: 506 * <pre> 507 * return formatter.format(this); 508 * </pre> 509 * 510 * @param formatter the formatter to use, not null 511 * @return the formatted date-time string, not null 512 * @throws DateTimeException if an error occurs during printing 513 */ format(DateTimeFormatter formatter)514 default String format(DateTimeFormatter formatter) { 515 Objects.requireNonNull(formatter, "formatter"); 516 return formatter.format(this); 517 } 518 519 //----------------------------------------------------------------------- 520 /** 521 * Converts this date-time to an {@code Instant}. 522 * <p> 523 * This returns an {@code Instant} representing the same point on the 524 * time-line as this date-time. The calculation combines the 525 * {@linkplain #toLocalDateTime() local date-time} and 526 * {@linkplain #getOffset() offset}. 527 * 528 * @return an {@code Instant} representing the same instant, not null 529 */ toInstant()530 default Instant toInstant() { 531 return Instant.ofEpochSecond(toEpochSecond(), toLocalTime().getNano()); 532 } 533 534 /** 535 * Converts this date-time to the number of seconds from the epoch 536 * of 1970-01-01T00:00:00Z. 537 * <p> 538 * This uses the {@linkplain #toLocalDateTime() local date-time} and 539 * {@linkplain #getOffset() offset} to calculate the epoch-second value, 540 * which is the number of elapsed seconds from 1970-01-01T00:00:00Z. 541 * Instants on the time-line after the epoch are positive, earlier are negative. 542 * 543 * @return the number of seconds from the epoch of 1970-01-01T00:00:00Z 544 */ toEpochSecond()545 default long toEpochSecond() { 546 long epochDay = toLocalDate().toEpochDay(); 547 long secs = epochDay * 86400 + toLocalTime().toSecondOfDay(); 548 secs -= getOffset().getTotalSeconds(); 549 return secs; 550 } 551 552 //----------------------------------------------------------------------- 553 /** 554 * Compares this date-time to another date-time, including the chronology. 555 * <p> 556 * The comparison is based first on the instant, then on the local date-time, 557 * then on the zone ID, then on the chronology. 558 * It is "consistent with equals", as defined by {@link Comparable}. 559 * <p> 560 * If all the date-time objects being compared are in the same chronology, then the 561 * additional chronology stage is not required. 562 * <p> 563 * This default implementation performs the comparison defined above. 564 * 565 * @param other the other date-time to compare to, not null 566 * @return the comparator value, negative if less, positive if greater 567 */ 568 @Override compareTo(ChronoZonedDateTime<?> other)569 default int compareTo(ChronoZonedDateTime<?> other) { 570 int cmp = Long.compare(toEpochSecond(), other.toEpochSecond()); 571 if (cmp == 0) { 572 cmp = toLocalTime().getNano() - other.toLocalTime().getNano(); 573 if (cmp == 0) { 574 cmp = toLocalDateTime().compareTo(other.toLocalDateTime()); 575 if (cmp == 0) { 576 cmp = getZone().getId().compareTo(other.getZone().getId()); 577 if (cmp == 0) { 578 cmp = getChronology().compareTo(other.getChronology()); 579 } 580 } 581 } 582 } 583 return cmp; 584 } 585 586 /** 587 * Checks if the instant of this date-time is before that of the specified date-time. 588 * <p> 589 * This method differs from the comparison in {@link #compareTo} in that it 590 * only compares the instant of the date-time. This is equivalent to using 591 * {@code dateTime1.toInstant().isBefore(dateTime2.toInstant());}. 592 * <p> 593 * This default implementation performs the comparison based on the epoch-second 594 * and nano-of-second. 595 * 596 * @param other the other date-time to compare to, not null 597 * @return true if this point is before the specified date-time 598 */ isBefore(ChronoZonedDateTime<?> other)599 default boolean isBefore(ChronoZonedDateTime<?> other) { 600 long thisEpochSec = toEpochSecond(); 601 long otherEpochSec = other.toEpochSecond(); 602 return thisEpochSec < otherEpochSec || 603 (thisEpochSec == otherEpochSec && toLocalTime().getNano() < other.toLocalTime().getNano()); 604 } 605 606 /** 607 * Checks if the instant of this date-time is after that of the specified date-time. 608 * <p> 609 * This method differs from the comparison in {@link #compareTo} in that it 610 * only compares the instant of the date-time. This is equivalent to using 611 * {@code dateTime1.toInstant().isAfter(dateTime2.toInstant());}. 612 * <p> 613 * This default implementation performs the comparison based on the epoch-second 614 * and nano-of-second. 615 * 616 * @param other the other date-time to compare to, not null 617 * @return true if this is after the specified date-time 618 */ isAfter(ChronoZonedDateTime<?> other)619 default boolean isAfter(ChronoZonedDateTime<?> other) { 620 long thisEpochSec = toEpochSecond(); 621 long otherEpochSec = other.toEpochSecond(); 622 return thisEpochSec > otherEpochSec || 623 (thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano()); 624 } 625 626 /** 627 * Checks if the instant of this date-time is equal to that of the specified date-time. 628 * <p> 629 * This method differs from the comparison in {@link #compareTo} and {@link #equals} 630 * in that it only compares the instant of the date-time. This is equivalent to using 631 * {@code dateTime1.toInstant().equals(dateTime2.toInstant());}. 632 * <p> 633 * This default implementation performs the comparison based on the epoch-second 634 * and nano-of-second. 635 * 636 * @param other the other date-time to compare to, not null 637 * @return true if the instant equals the instant of the specified date-time 638 */ isEqual(ChronoZonedDateTime<?> other)639 default boolean isEqual(ChronoZonedDateTime<?> other) { 640 return toEpochSecond() == other.toEpochSecond() && 641 toLocalTime().getNano() == other.toLocalTime().getNano(); 642 } 643 644 //----------------------------------------------------------------------- 645 /** 646 * Checks if this date-time is equal to another date-time. 647 * <p> 648 * The comparison is based on the offset date-time and the zone. 649 * To compare for the same instant on the time-line, use {@link #compareTo}. 650 * Only objects of type {@code ChronoZonedDateTime} are compared, other types return false. 651 * 652 * @param obj the object to check, null returns false 653 * @return true if this is equal to the other date-time 654 */ 655 @Override equals(Object obj)656 boolean equals(Object obj); 657 658 /** 659 * A hash code for this date-time. 660 * 661 * @return a suitable hash code 662 */ 663 @Override hashCode()664 int hashCode(); 665 666 //----------------------------------------------------------------------- 667 /** 668 * Outputs this date-time as a {@code String}. 669 * <p> 670 * The output will include the full zoned date-time. 671 * 672 * @return a string representation of this date-time, not null 673 */ 674 @Override toString()675 String toString(); 676 677 } 678