1 /*
   2  * Copyright (c) 2012, 2013, 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;
  63 
  64 import static java.time.LocalTime.SECONDS_PER_DAY;
  65 import static java.time.LocalTime.SECONDS_PER_HOUR;
  66 import static java.time.LocalTime.SECONDS_PER_MINUTE;
  67 import static java.time.temporal.ChronoField.INSTANT_SECONDS;
  68 import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
  69 import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
  70 import static java.time.temporal.ChronoField.NANO_OF_SECOND;
  71 import static java.time.temporal.ChronoUnit.NANOS;
  72 
  73 import java.io.DataInput;
  74 import java.io.DataOutput;
  75 import java.io.IOException;
  76 import java.io.InvalidObjectException;
  77 import java.io.ObjectStreamException;
  78 import java.io.Serializable;
  79 import java.time.format.DateTimeFormatters;
  80 import java.time.format.DateTimeParseException;
  81 import java.time.temporal.ChronoField;
  82 import java.time.temporal.ChronoUnit;
  83 import java.time.temporal.Queries;
  84 import java.time.temporal.Temporal;
  85 import java.time.temporal.TemporalAccessor;
  86 import java.time.temporal.TemporalAdder;
  87 import java.time.temporal.TemporalAdjuster;
  88 import java.time.temporal.TemporalField;
  89 import java.time.temporal.TemporalQuery;
  90 import java.time.temporal.TemporalSubtractor;
  91 import java.time.temporal.TemporalUnit;
  92 import java.time.temporal.ValueRange;
  93 import java.util.Objects;
  94 
  95 /**
  96  * An instantaneous point on the time-line.
  97  * <p>
  98  * This class models a single instantaneous point on the time-line.
  99  * This might be used to record event time-stamps in the application.
 100  * <p>
 101  * For practicality, the instant is stored with some constraints.
 102  * The measurable time-line is restricted to the number of seconds that can be held
 103  * in a {@code long}. This is greater than the current estimated age of the universe.
 104  * The instant is stored to nanosecond resolution.
 105  * <p>
 106  * The range of an instant requires the storage of a number larger than a {@code long}.
 107  * To achieve this, the class stores a {@code long} representing epoch-seconds and an
 108  * {@code int} representing nanosecond-of-second, which will always be between 0 and 999,999,999.
 109  * The epoch-seconds are measured from the standard Java epoch of {@code 1970-01-01T00:00:00Z}
 110  * where instants after the epoch have positive values, and earlier instants have negative values.
 111  * For both the epoch-second and nanosecond parts, a larger value is always later on the time-line
 112  * than a smaller value.
 113  *
 114  * <h3>Time-scale</h3>
 115  * <p>
 116  * The length of the solar day is the standard way that humans measure time.
 117  * This has traditionally been subdivided into 24 hours of 60 minutes of 60 seconds,
 118  * forming a 86400 second day.
 119  * <p>
 120  * Modern timekeeping is based on atomic clocks which precisely define an SI second
 121  * relative to the transitions of a Caesium atom. The length of an SI second was defined
 122  * to be very close to the 86400th fraction of a day.
 123  * <p>
 124  * Unfortunately, as the Earth rotates the length of the day varies.
 125  * In addition, over time the average length of the day is getting longer as the Earth slows.
 126  * As a result, the length of a solar day in 2012 is slightly longer than 86400 SI seconds.
 127  * The actual length of any given day and the amount by which the Earth is slowing
 128  * are not predictable and can only be determined by measurement.
 129  * The UT1 time-scale captures the accurate length of day, but is only available some
 130  * time after the day has completed.
 131  * <p>
 132  * The UTC time-scale is a standard approach to bundle up all the additional fractions
 133  * of a second from UT1 into whole seconds, known as <i>leap-seconds</i>.
 134  * A leap-second may be added or removed depending on the Earth's rotational changes.
 135  * As such, UTC permits a day to have 86399 SI seconds or 86401 SI seconds where
 136  * necessary in order to keep the day aligned with the Sun.
 137  * <p>
 138  * The modern UTC time-scale was introduced in 1972, introducing the concept of whole leap-seconds.
 139  * Between 1958 and 1972, the definition of UTC was complex, with minor sub-second leaps and
 140  * alterations to the length of the notional second. As of 2012, discussions are underway
 141  * to change the definition of UTC again, with the potential to remove leap seconds or
 142  * introduce other changes.
 143  * <p>
 144  * Given the complexity of accurate timekeeping described above, this Java API defines
 145  * its own time-scale with a simplification. The Java time-scale is defined as follows:
 146  * <p><ul>
 147  * <li>midday will always be exactly as defined by the agreed international civil time</li>
 148  * <li>other times during the day will be broadly in line with the agreed international civil time</li>
 149  * <li>the day will be divided into exactly 86400 subdivisions, referred to as "seconds"</li>
 150  * <li>the Java "second" may differ from an SI second</li>
 151  * </ul><p>
 152  * Agreed international civil time is the base time-scale agreed by international convention,
 153  * which in 2012 is UTC (with leap-seconds).
 154  * <p>
 155  * In 2012, the definition of the Java time-scale is the same as UTC for all days except
 156  * those where a leap-second occurs. On days where a leap-second does occur, the time-scale
 157  * effectively eliminates the leap-second, maintaining the fiction of 86400 seconds in the day.
 158  * <p>
 159  * The main benefit of always dividing the day into 86400 subdivisions is that it matches the
 160  * expectations of most users of the API. The alternative is to force every user to understand
 161  * what a leap second is and to force them to have special logic to handle them.
 162  * Most applications do not have access to a clock that is accurate enough to record leap-seconds.
 163  * Most applications also do not have a problem with a second being a very small amount longer or
 164  * shorter than a real SI second during a leap-second.
 165  * <p>
 166  * If an application does have access to an accurate clock that reports leap-seconds, then the
 167  * recommended technique to implement the Java time-scale is to use the UTC-SLS convention.
 168  * <a href="http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/">UTC-SLS</a> effectively smoothes the
 169  * leap-second over the last 1000 seconds of the day, making each of the last 1000 "seconds"
 170  * 1/1000th longer or shorter than a real SI second.
 171  * <p>
 172  * One final problem is the definition of the agreed international civil time before the
 173  * introduction of modern UTC in 1972. This includes the Java epoch of {@code 1970-01-01}.
 174  * It is intended that instants before 1972 be interpreted based on the solar day divided
 175  * into 86400 subdivisions.
 176  * <p>
 177  * The Java time-scale is used for all date-time classes.
 178  * This includes {@code Instant}, {@code LocalDate}, {@code LocalTime}, {@code OffsetDateTime},
 179  * {@code ZonedDateTime} and {@code Duration}.
 180  *
 181  * <h3>Specification for implementors</h3>
 182  * This class is immutable and thread-safe.
 183  *
 184  * @since 1.8
 185  */
 186 public final class Instant
 187         implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable {
 188 
 189     /**
 190      * Constant for the 1970-01-01T00:00:00Z epoch instant.
 191      */
 192     public static final Instant EPOCH = new Instant(0, 0);
 193     /**
 194      * The minimum supported epoch second.
 195      */
 196     private static final long MIN_SECOND = -31557014167219200L;
 197     /**
 198      * The maximum supported epoch second.
 199      */
 200     private static final long MAX_SECOND = 31556889864403199L;
 201     /**
 202      * The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
 203      * This could be used by an application as a "far past" instant.
 204      * <p>
 205      * This is one year earlier than the minimum {@code LocalDateTime}.
 206      * This provides sufficient values to handle the range of {@code ZoneOffset}
 207      * which affect the instant in addition to the local date-time.
 208      * The value is also chosen such that the value of the year fits in
 209      * an {@code int}.
 210      */
 211     public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
 212     /**
 213      * The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
 214      * This could be used by an application as a "far future" instant.
 215      * <p>
 216      * This is one year later than the maximum {@code LocalDateTime}.
 217      * This provides sufficient values to handle the range of {@code ZoneOffset}
 218      * which affect the instant in addition to the local date-time.
 219      * The value is also chosen such that the value of the year fits in
 220      * an {@code int}.
 221      */
 222     public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
 223 
 224     /**
 225      * Serialization version.
 226      */
 227     private static final long serialVersionUID = -665713676816604388L;
 228     /**
 229      * Constant for nanos per second.
 230      */
 231     private static final int NANOS_PER_SECOND = 1000_000_000;
 232 
 233     /**
 234      * The number of seconds from the epoch of 1970-01-01T00:00:00Z.
 235      */
 236     private final long seconds;
 237     /**
 238      * The number of nanoseconds, later along the time-line, from the seconds field.
 239      * This is always positive, and never exceeds 999,999,999.
 240      */
 241     private final int nanos;
 242 
 243     //-----------------------------------------------------------------------
 244     /**
 245      * Obtains the current instant from the system clock.
 246      * <p>
 247      * This will query the {@link Clock#systemUTC() system UTC clock} to
 248      * obtain the current instant.
 249      * <p>
 250      * Using this method will prevent the ability to use an alternate time-source for
 251      * testing because the clock is effectively hard-coded.
 252      *
 253      * @return the current instant using the system clock, not null
 254      */
 255     public static Instant now() {
 256         return Clock.systemUTC().instant();
 257     }
 258 
 259     /**
 260      * Obtains the current instant from the specified clock.
 261      * <p>
 262      * This will query the specified clock to obtain the current time.
 263      * <p>
 264      * Using this method allows the use of an alternate clock for testing.
 265      * The alternate clock may be introduced using {@link Clock dependency injection}.
 266      *
 267      * @param clock  the clock to use, not null
 268      * @return the current instant, not null
 269      */
 270     public static Instant now(Clock clock) {
 271         Objects.requireNonNull(clock, "clock");
 272         return clock.instant();
 273     }
 274 
 275     //-----------------------------------------------------------------------
 276     /**
 277      * Obtains an instance of {@code Instant} using seconds from the
 278      * epoch of 1970-01-01T00:00:00Z.
 279      * <p>
 280      * The nanosecond field is set to zero.
 281      *
 282      * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
 283      * @return an instant, not null
 284      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
 285      */
 286     public static Instant ofEpochSecond(long epochSecond) {
 287         return create(epochSecond, 0);
 288     }
 289 
 290     /**
 291      * Obtains an instance of {@code Instant} using seconds from the
 292      * epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
 293      * <p>
 294      * This method allows an arbitrary number of nanoseconds to be passed in.
 295      * The factory will alter the values of the second and nanosecond in order
 296      * to ensure that the stored nanosecond is in the range 0 to 999,999,999.
 297      * For example, the following will result in the exactly the same instant:
 298      * <pre>
 299      *  Instant.ofSeconds(3, 1);
 300      *  Instant.ofSeconds(4, -999_999_999);
 301      *  Instant.ofSeconds(2, 1000_000_001);
 302      * </pre>
 303      *
 304      * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
 305      * @param nanoAdjustment  the nanosecond adjustment to the number of seconds, positive or negative
 306      * @return an instant, not null
 307      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
 308      * @throws ArithmeticException if numeric overflow occurs
 309      */
 310     public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
 311         long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
 312         int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
 313         return create(secs, nos);
 314     }
 315 
 316     /**
 317      * Obtains an instance of {@code Instant} using milliseconds from the
 318      * epoch of 1970-01-01T00:00:00Z.
 319      * <p>
 320      * The seconds and nanoseconds are extracted from the specified milliseconds.
 321      *
 322      * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
 323      * @return an instant, not null
 324      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
 325      */
 326     public static Instant ofEpochMilli(long epochMilli) {
 327         long secs = Math.floorDiv(epochMilli, 1000);
 328         int mos = (int)Math.floorMod(epochMilli, 1000);
 329         return create(secs, mos * 1000_000);
 330     }
 331 
 332     //-----------------------------------------------------------------------
 333     /**
 334      * Obtains an instance of {@code Instant} from a temporal object.
 335      * <p>
 336      * A {@code TemporalAccessor} represents some form of date and time information.
 337      * This factory converts the arbitrary temporal object to an instance of {@code Instant}.
 338      * <p>
 339      * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
 340      * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
 341      * <p>
 342      * This method matches the signature of the functional interface {@link TemporalQuery}
 343      * allowing it to be used as a query via method reference, {@code Instant::from}.
 344      *
 345      * @param temporal  the temporal object to convert, not null
 346      * @return the instant, not null
 347      * @throws DateTimeException if unable to convert to an {@code Instant}
 348      */
 349     public static Instant from(TemporalAccessor temporal) {
 350         long instantSecs = temporal.getLong(INSTANT_SECONDS);
 351         int nanoOfSecond = temporal.get(NANO_OF_SECOND);
 352         return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
 353     }
 354 
 355     //-----------------------------------------------------------------------
 356     /**
 357      * Obtains an instance of {@code Instant} from a text string such as
 358      * {@code 2007-12-03T10:15:30:00}.
 359      * <p>
 360      * The string must represent a valid instant in UTC and is parsed using
 361      * {@link DateTimeFormatters#isoInstant()}.
 362      *
 363      * @param text  the text to parse, not null
 364      * @return the parsed instant, not null
 365      * @throws DateTimeParseException if the text cannot be parsed
 366      */
 367     public static Instant parse(final CharSequence text) {
 368         return DateTimeFormatters.isoInstant().parse(text, Instant::from);
 369     }
 370 
 371     //-----------------------------------------------------------------------
 372     /**
 373      * Obtains an instance of {@code Instant} using seconds and nanoseconds.
 374      *
 375      * @param seconds  the length of the duration in seconds
 376      * @param nanoOfSecond  the nano-of-second, from 0 to 999,999,999
 377      * @throws DateTimeException if the instant exceeds the maximum or minimum instant
 378      */
 379     private static Instant create(long seconds, int nanoOfSecond) {
 380         if ((seconds | nanoOfSecond) == 0) {
 381             return EPOCH;
 382         }
 383         if (seconds < MIN_SECOND || seconds > MAX_SECOND) {
 384             throw new DateTimeException("Instant exceeds minimum or maximum instant");
 385         }
 386         return new Instant(seconds, nanoOfSecond);
 387     }
 388 
 389     /**
 390      * Constructs an instance of {@code Instant} using seconds from the epoch of
 391      * 1970-01-01T00:00:00Z and nanosecond fraction of second.
 392      *
 393      * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
 394      * @param nanos  the nanoseconds within the second, must be positive
 395      */
 396     private Instant(long epochSecond, int nanos) {
 397         super();
 398         this.seconds = epochSecond;
 399         this.nanos = nanos;
 400     }
 401 
 402     //-----------------------------------------------------------------------
 403     /**
 404      * Checks if the specified field is supported.
 405      * <p>
 406      * This checks if this instant can be queried for the specified field.
 407      * If false, then calling the {@link #range(TemporalField) range} and
 408      * {@link #get(TemporalField) get} methods will throw an exception.
 409      * <p>
 410      * If the field is a {@link ChronoField} then the query is implemented here.
 411      * The supported fields are:
 412      * <ul>
 413      * <li>{@code NANO_OF_SECOND}
 414      * <li>{@code MICRO_OF_SECOND}
 415      * <li>{@code MILLI_OF_SECOND}
 416      * <li>{@code INSTANT_SECONDS}
 417      * </ul>
 418      * All other {@code ChronoField} instances will return false.
 419      * <p>
 420      * If the field is not a {@code ChronoField}, then the result of this method
 421      * is obtained by invoking {@code TemporalField.doIsSupported(TemporalAccessor)}
 422      * passing {@code this} as the argument.
 423      * Whether the field is supported is determined by the field.
 424      *
 425      * @param field  the field to check, null returns false
 426      * @return true if the field is supported on this instant, false if not
 427      */
 428     @Override
 429     public boolean isSupported(TemporalField field) {
 430         if (field instanceof ChronoField) {
 431             return field == INSTANT_SECONDS || field == NANO_OF_SECOND || field == MICRO_OF_SECOND || field == MILLI_OF_SECOND;
 432         }
 433         return field != null && field.doIsSupported(this);
 434     }
 435 
 436     /**
 437      * Gets the range of valid values for the specified field.
 438      * <p>
 439      * The range object expresses the minimum and maximum valid values for a field.
 440      * This instant is used to enhance the accuracy of the returned range.
 441      * If it is not possible to return the range, because the field is not supported
 442      * or for some other reason, an exception is thrown.
 443      * <p>
 444      * If the field is a {@link ChronoField} then the query is implemented here.
 445      * The {@link #isSupported(TemporalField) supported fields} will return
 446      * appropriate range instances.
 447      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 448      * <p>
 449      * If the field is not a {@code ChronoField}, then the result of this method
 450      * is obtained by invoking {@code TemporalField.doRange(TemporalAccessor)}
 451      * passing {@code this} as the argument.
 452      * Whether the range can be obtained is determined by the field.
 453      *
 454      * @param field  the field to query the range for, not null
 455      * @return the range of valid values for the field, not null
 456      * @throws DateTimeException if the range for the field cannot be obtained
 457      */
 458     @Override  // override for Javadoc
 459     public ValueRange range(TemporalField field) {
 460         return Temporal.super.range(field);
 461     }
 462 
 463     /**
 464      * Gets the value of the specified field from this instant as an {@code int}.
 465      * <p>
 466      * This queries this instant for the value for the specified field.
 467      * The returned value will always be within the valid range of values for the field.
 468      * If it is not possible to return the value, because the field is not supported
 469      * or for some other reason, an exception is thrown.
 470      * <p>
 471      * If the field is a {@link ChronoField} then the query is implemented here.
 472      * The {@link #isSupported(TemporalField) supported fields} will return valid
 473      * values based on this date-time, except {@code INSTANT_SECONDS} which is too
 474      * large to fit in an {@code int} and throws a {@code DateTimeException}.
 475      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 476      * <p>
 477      * If the field is not a {@code ChronoField}, then the result of this method
 478      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 479      * passing {@code this} as the argument. Whether the value can be obtained,
 480      * and what the value represents, is determined by the field.
 481      *
 482      * @param field  the field to get, not null
 483      * @return the value for the field
 484      * @throws DateTimeException if a value for the field cannot be obtained
 485      * @throws ArithmeticException if numeric overflow occurs
 486      */
 487     @Override  // override for Javadoc and performance
 488     public int get(TemporalField field) {
 489         if (field instanceof ChronoField) {
 490             switch ((ChronoField) field) {
 491                 case NANO_OF_SECOND: return nanos;
 492                 case MICRO_OF_SECOND: return nanos / 1000;
 493                 case MILLI_OF_SECOND: return nanos / 1000_000;
 494                 case INSTANT_SECONDS: INSTANT_SECONDS.checkValidIntValue(seconds);
 495             }
 496             throw new DateTimeException("Unsupported field: " + field.getName());
 497         }
 498         return range(field).checkValidIntValue(field.doGet(this), field);
 499     }
 500 
 501     /**
 502      * Gets the value of the specified field from this instant as a {@code long}.
 503      * <p>
 504      * This queries this instant for the value for the specified field.
 505      * If it is not possible to return the value, because the field is not supported
 506      * or for some other reason, an exception is thrown.
 507      * <p>
 508      * If the field is a {@link ChronoField} then the query is implemented here.
 509      * The {@link #isSupported(TemporalField) supported fields} will return valid
 510      * values based on this date-time.
 511      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 512      * <p>
 513      * If the field is not a {@code ChronoField}, then the result of this method
 514      * is obtained by invoking {@code TemporalField.doGet(TemporalAccessor)}
 515      * passing {@code this} as the argument. Whether the value can be obtained,
 516      * and what the value represents, is determined by the field.
 517      *
 518      * @param field  the field to get, not null
 519      * @return the value for the field
 520      * @throws DateTimeException if a value for the field cannot be obtained
 521      * @throws ArithmeticException if numeric overflow occurs
 522      */
 523     @Override
 524     public long getLong(TemporalField field) {
 525         if (field instanceof ChronoField) {
 526             switch ((ChronoField) field) {
 527                 case NANO_OF_SECOND: return nanos;
 528                 case MICRO_OF_SECOND: return nanos / 1000;
 529                 case MILLI_OF_SECOND: return nanos / 1000_000;
 530                 case INSTANT_SECONDS: return seconds;
 531             }
 532             throw new DateTimeException("Unsupported field: " + field.getName());
 533         }
 534         return field.doGet(this);
 535     }
 536 
 537     //-----------------------------------------------------------------------
 538     /**
 539      * Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
 540      * <p>
 541      * The epoch second count is a simple incrementing count of seconds where
 542      * second 0 is 1970-01-01T00:00:00Z.
 543      * The nanosecond part of the day is returned by {@code getNanosOfSecond}.
 544      *
 545      * @return the seconds from the epoch of 1970-01-01T00:00:00Z
 546      */
 547     public long getEpochSecond() {
 548         return seconds;
 549     }
 550 
 551     /**
 552      * Gets the number of nanoseconds, later along the time-line, from the start
 553      * of the second.
 554      * <p>
 555      * The nanosecond-of-second value measures the total number of nanoseconds from
 556      * the second returned by {@code getEpochSecond}.
 557      *
 558      * @return the nanoseconds within the second, always positive, never exceeds 999,999,999
 559      */
 560     public int getNano() {
 561         return nanos;
 562     }
 563 
 564     //-------------------------------------------------------------------------
 565     /**
 566      * Returns an adjusted copy of this instant.
 567      * <p>
 568      * This returns a new {@code Instant}, based on this one, with the date adjusted.
 569      * The adjustment takes place using the specified adjuster strategy object.
 570      * Read the documentation of the adjuster to understand what adjustment will be made.
 571      * <p>
 572      * The result of this method is obtained by invoking the
 573      * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
 574      * specified adjuster passing {@code this} as the argument.
 575      * <p>
 576      * This instance is immutable and unaffected by this method call.
 577      *
 578      * @param adjuster the adjuster to use, not null
 579      * @return an {@code Instant} based on {@code this} with the adjustment made, not null
 580      * @throws DateTimeException if the adjustment cannot be made
 581      * @throws ArithmeticException if numeric overflow occurs
 582      */
 583     @Override
 584     public Instant with(TemporalAdjuster adjuster) {
 585         return (Instant) adjuster.adjustInto(this);
 586     }
 587 
 588     /**
 589      * Returns a copy of this instant with the specified field set to a new value.
 590      * <p>
 591      * This returns a new {@code Instant}, based on this one, with the value
 592      * for the specified field changed.
 593      * If it is not possible to set the value, because the field is not supported or for
 594      * some other reason, an exception is thrown.
 595      * <p>
 596      * If the field is a {@link ChronoField} then the adjustment is implemented here.
 597      * The supported fields behave as follows:
 598      * <ul>
 599      * <li>{@code NANO_OF_SECOND} -
 600      *  Returns an {@code Instant} with the specified nano-of-second.
 601      *  The epoch-second will be unchanged.
 602      * <li>{@code MICRO_OF_SECOND} -
 603      *  Returns an {@code Instant} with the nano-of-second replaced by the specified
 604      *  micro-of-second multiplied by 1,000. The epoch-second will be unchanged.
 605      * <li>{@code MILLI_OF_SECOND} -
 606      *  Returns an {@code Instant} with the nano-of-second replaced by the specified
 607      *  milli-of-second multiplied by 1,000,000. The epoch-second will be unchanged.
 608      * <li>{@code INSTANT_SECONDS} -
 609      *  Returns an {@code Instant} with the specified epoch-second.
 610      *  The nano-of-second will be unchanged.
 611      * </ul>
 612      * <p>
 613      * In all cases, if the new value is outside the valid range of values for the field
 614      * then a {@code DateTimeException} will be thrown.
 615      * <p>
 616      * All other {@code ChronoField} instances will throw a {@code DateTimeException}.
 617      * <p>
 618      * If the field is not a {@code ChronoField}, then the result of this method
 619      * is obtained by invoking {@code TemporalField.doWith(Temporal, long)}
 620      * passing {@code this} as the argument. In this case, the field determines
 621      * whether and how to adjust the instant.
 622      * <p>
 623      * This instance is immutable and unaffected by this method call.
 624      *
 625      * @param field  the field to set in the result, not null
 626      * @param newValue  the new value of the field in the result
 627      * @return an {@code Instant} based on {@code this} with the specified field set, not null
 628      * @throws DateTimeException if the field cannot be set
 629      * @throws ArithmeticException if numeric overflow occurs
 630      */
 631     @Override
 632     public Instant with(TemporalField field, long newValue) {
 633         if (field instanceof ChronoField) {
 634             ChronoField f = (ChronoField) field;
 635             f.checkValidValue(newValue);
 636             switch (f) {
 637                 case MILLI_OF_SECOND: {
 638                     int nval = (int) newValue * 1000_000;
 639                     return (nval != nanos ? create(seconds, nval) : this);
 640                 }
 641                 case MICRO_OF_SECOND: {
 642                     int nval = (int) newValue * 1000;
 643                     return (nval != nanos ? create(seconds, nval) : this);
 644                 }
 645                 case NANO_OF_SECOND: return (newValue != nanos ? create(seconds, (int) newValue) : this);
 646                 case INSTANT_SECONDS: return (newValue != seconds ? create(newValue, nanos) : this);
 647             }
 648             throw new DateTimeException("Unsupported field: " + field.getName());
 649         }
 650         return field.doWith(this, newValue);
 651     }
 652 
 653     //-----------------------------------------------------------------------
 654     /**
 655      * {@inheritDoc}
 656      * @throws DateTimeException {@inheritDoc}
 657      * @throws ArithmeticException {@inheritDoc}
 658      */
 659     @Override
 660     public Instant plus(TemporalAdder adder) {
 661         return (Instant) adder.addTo(this);
 662     }
 663 
 664     /**
 665      * {@inheritDoc}
 666      * @throws DateTimeException {@inheritDoc}
 667      * @throws ArithmeticException {@inheritDoc}
 668      */
 669     @Override
 670     public Instant plus(long amountToAdd, TemporalUnit unit) {
 671         if (unit instanceof ChronoUnit) {
 672             switch ((ChronoUnit) unit) {
 673                 case NANOS: return plusNanos(amountToAdd);
 674                 case MICROS: return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
 675                 case MILLIS: return plusMillis(amountToAdd);
 676                 case SECONDS: return plusSeconds(amountToAdd);
 677                 case MINUTES: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE));
 678                 case HOURS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR));
 679                 case HALF_DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2));
 680                 case DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY));
 681             }
 682             throw new DateTimeException("Unsupported unit: " + unit.getName());
 683         }
 684         return unit.doPlus(this, amountToAdd);
 685     }
 686 
 687     //-----------------------------------------------------------------------
 688     /**
 689      * Returns a copy of this instant with the specified duration in seconds added.
 690      * <p>
 691      * This instance is immutable and unaffected by this method call.
 692      *
 693      * @param secondsToAdd  the seconds to add, positive or negative
 694      * @return an {@code Instant} based on this instant with the specified seconds added, not null
 695      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 696      * @throws ArithmeticException if numeric overflow occurs
 697      */
 698     public Instant plusSeconds(long secondsToAdd) {
 699         return plus(secondsToAdd, 0);
 700     }
 701 
 702     /**
 703      * Returns a copy of this instant with the specified duration in milliseconds added.
 704      * <p>
 705      * This instance is immutable and unaffected by this method call.
 706      *
 707      * @param millisToAdd  the milliseconds to add, positive or negative
 708      * @return an {@code Instant} based on this instant with the specified milliseconds added, not null
 709      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 710      * @throws ArithmeticException if numeric overflow occurs
 711      */
 712     public Instant plusMillis(long millisToAdd) {
 713         return plus(millisToAdd / 1000, (millisToAdd % 1000) * 1000_000);
 714     }
 715 
 716     /**
 717      * Returns a copy of this instant with the specified duration in nanoseconds added.
 718      * <p>
 719      * This instance is immutable and unaffected by this method call.
 720      *
 721      * @param nanosToAdd  the nanoseconds to add, positive or negative
 722      * @return an {@code Instant} based on this instant with the specified nanoseconds added, not null
 723      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 724      * @throws ArithmeticException if numeric overflow occurs
 725      */
 726     public Instant plusNanos(long nanosToAdd) {
 727         return plus(0, nanosToAdd);
 728     }
 729 
 730     /**
 731      * Returns a copy of this instant with the specified duration added.
 732      * <p>
 733      * This instance is immutable and unaffected by this method call.
 734      *
 735      * @param secondsToAdd  the seconds to add, positive or negative
 736      * @param nanosToAdd  the nanos to add, positive or negative
 737      * @return an {@code Instant} based on this instant with the specified seconds added, not null
 738      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 739      * @throws ArithmeticException if numeric overflow occurs
 740      */
 741     private Instant plus(long secondsToAdd, long nanosToAdd) {
 742         if ((secondsToAdd | nanosToAdd) == 0) {
 743             return this;
 744         }
 745         long epochSec = Math.addExact(seconds, secondsToAdd);
 746         epochSec = Math.addExact(epochSec, nanosToAdd / NANOS_PER_SECOND);
 747         nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
 748         long nanoAdjustment = nanos + nanosToAdd;  // safe int+NANOS_PER_SECOND
 749         return ofEpochSecond(epochSec, nanoAdjustment);
 750     }
 751 
 752     //-----------------------------------------------------------------------
 753     /**
 754      * {@inheritDoc}
 755      * @throws DateTimeException {@inheritDoc}
 756      * @throws ArithmeticException {@inheritDoc}
 757      */
 758     @Override
 759     public Instant minus(TemporalSubtractor subtractor) {
 760         return (Instant) subtractor.subtractFrom(this);
 761     }
 762 
 763     /**
 764      * {@inheritDoc}
 765      * @throws DateTimeException {@inheritDoc}
 766      * @throws ArithmeticException {@inheritDoc}
 767      */
 768     @Override
 769     public Instant minus(long amountToSubtract, TemporalUnit unit) {
 770         return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
 771     }
 772 
 773     //-----------------------------------------------------------------------
 774     /**
 775      * Returns a copy of this instant with the specified duration in seconds subtracted.
 776      * <p>
 777      * This instance is immutable and unaffected by this method call.
 778      *
 779      * @param secondsToSubtract  the seconds to subtract, positive or negative
 780      * @return an {@code Instant} based on this instant with the specified seconds subtracted, not null
 781      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 782      * @throws ArithmeticException if numeric overflow occurs
 783      */
 784     public Instant minusSeconds(long secondsToSubtract) {
 785         if (secondsToSubtract == Long.MIN_VALUE) {
 786             return plusSeconds(Long.MAX_VALUE).plusSeconds(1);
 787         }
 788         return plusSeconds(-secondsToSubtract);
 789     }
 790 
 791     /**
 792      * Returns a copy of this instant with the specified duration in milliseconds subtracted.
 793      * <p>
 794      * This instance is immutable and unaffected by this method call.
 795      *
 796      * @param millisToSubtract  the milliseconds to subtract, positive or negative
 797      * @return an {@code Instant} based on this instant with the specified milliseconds subtracted, not null
 798      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 799      * @throws ArithmeticException if numeric overflow occurs
 800      */
 801     public Instant minusMillis(long millisToSubtract) {
 802         if (millisToSubtract == Long.MIN_VALUE) {
 803             return plusMillis(Long.MAX_VALUE).plusMillis(1);
 804         }
 805         return plusMillis(-millisToSubtract);
 806     }
 807 
 808     /**
 809      * Returns a copy of this instant with the specified duration in nanoseconds subtracted.
 810      * <p>
 811      * This instance is immutable and unaffected by this method call.
 812      *
 813      * @param nanosToSubtract  the nanoseconds to subtract, positive or negative
 814      * @return an {@code Instant} based on this instant with the specified nanoseconds subtracted, not null
 815      * @throws DateTimeException if the result exceeds the maximum or minimum instant
 816      * @throws ArithmeticException if numeric overflow occurs
 817      */
 818     public Instant minusNanos(long nanosToSubtract) {
 819         if (nanosToSubtract == Long.MIN_VALUE) {
 820             return plusNanos(Long.MAX_VALUE).plusNanos(1);
 821         }
 822         return plusNanos(-nanosToSubtract);
 823     }
 824 
 825     //-------------------------------------------------------------------------
 826     /**
 827      * Queries this instant using the specified query.
 828      * <p>
 829      * This queries this instant using the specified query strategy object.
 830      * The {@code TemporalQuery} object defines the logic to be used to
 831      * obtain the result. Read the documentation of the query to understand
 832      * what the result of this method will be.
 833      * <p>
 834      * The result of this method is obtained by invoking the
 835      * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
 836      * specified query passing {@code this} as the argument.
 837      *
 838      * @param <R> the type of the result
 839      * @param query  the query to invoke, not null
 840      * @return the query result, null may be returned (defined by the query)
 841      * @throws DateTimeException if unable to query (defined by the query)
 842      * @throws ArithmeticException if numeric overflow occurs (defined by the query)
 843      */
 844     @SuppressWarnings("unchecked")
 845     @Override
 846     public <R> R query(TemporalQuery<R> query) {
 847         if (query == Queries.precision()) {
 848             return (R) NANOS;
 849         }
 850         // inline TemporalAccessor.super.query(query) as an optimization
 851         if (query == Queries.chrono() || query == Queries.zoneId() || query == Queries.zone() || query == Queries.offset()) {
 852             return null;
 853         }
 854         return query.queryFrom(this);
 855     }
 856 
 857     /**
 858      * Adjusts the specified temporal object to have this instant.
 859      * <p>
 860      * This returns a temporal object of the same observable type as the input
 861      * with the instant changed to be the same as this.
 862      * <p>
 863      * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
 864      * twice, passing {@link ChronoField#INSTANT_SECONDS} and
 865      * {@link ChronoField#NANO_OF_SECOND} as the fields.
 866      * <p>
 867      * In most cases, it is clearer to reverse the calling pattern by using
 868      * {@link Temporal#with(TemporalAdjuster)}:
 869      * <pre>
 870      *   // these two lines are equivalent, but the second approach is recommended
 871      *   temporal = thisInstant.adjustInto(temporal);
 872      *   temporal = temporal.with(thisInstant);
 873      * </pre>
 874      * <p>
 875      * This instance is immutable and unaffected by this method call.
 876      *
 877      * @param temporal  the target object to be adjusted, not null
 878      * @return the adjusted object, not null
 879      * @throws DateTimeException if unable to make the adjustment
 880      * @throws ArithmeticException if numeric overflow occurs
 881      */
 882     @Override
 883     public Temporal adjustInto(Temporal temporal) {
 884         return temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
 885     }
 886 
 887     /**
 888      * Calculates the period between this instant and another instant in
 889      * terms of the specified unit.
 890      * <p>
 891      * This calculates the period between two instants in terms of a single unit.
 892      * The start and end points are {@code this} and the specified instant.
 893      * The result will be negative if the end is before the start.
 894      * The calculation returns a whole number, representing the number of
 895      * complete units between the two instants.
 896      * The {@code Temporal} passed to this method must be an {@code Instant}.
 897      * For example, the period in days between two dates can be calculated
 898      * using {@code startInstant.periodUntil(endInstant, SECONDS)}.
 899      * <p>
 900      * This method operates in association with {@link TemporalUnit#between}.
 901      * The result of this method is a {@code long} representing the amount of
 902      * the specified unit. By contrast, the result of {@code between} is an
 903      * object that can be used directly in addition/subtraction:
 904      * <pre>
 905      *   long period = start.periodUntil(end, SECONDS);   // this method
 906      *   dateTime.plus(SECONDS.between(start, end));      // use in plus/minus
 907      * </pre>
 908      * <p>
 909      * The calculation is implemented in this method for {@link ChronoUnit}.
 910      * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
 911      * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS}
 912      * are supported. Other {@code ChronoUnit} values will throw an exception.
 913      * <p>
 914      * If the unit is not a {@code ChronoUnit}, then the result of this method
 915      * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
 916      * passing {@code this} as the first argument and the input temporal as
 917      * the second argument.
 918      * <p>
 919      * This instance is immutable and unaffected by this method call.
 920      *
 921      * @param endInstant  the end date, which must be a {@code LocalDate}, not null
 922      * @param unit  the unit to measure the period in, not null
 923      * @return the amount of the period between this date and the end date
 924      * @throws DateTimeException if the period cannot be calculated
 925      * @throws ArithmeticException if numeric overflow occurs
 926      */
 927     @Override
 928     public long periodUntil(Temporal endInstant, TemporalUnit unit) {
 929         if (endInstant instanceof Instant == false) {
 930             Objects.requireNonNull(endInstant, "endInstant");
 931             throw new DateTimeException("Unable to calculate period between objects of two different types");
 932         }
 933         Instant end = (Instant) endInstant;
 934         if (unit instanceof ChronoUnit) {
 935             ChronoUnit f = (ChronoUnit) unit;
 936             switch (f) {
 937                 case NANOS: return nanosUntil(end);
 938                 case MICROS: return nanosUntil(end) / 1000;
 939                 case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli());
 940                 case SECONDS: return secondsUntil(end);
 941                 case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE;
 942                 case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR;
 943                 case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR);
 944                 case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY);
 945             }
 946             throw new DateTimeException("Unsupported unit: " + unit.getName());
 947         }
 948         return unit.between(this, endInstant).getAmount();
 949     }
 950 
 951     private long nanosUntil(Instant end) {
 952         long secs = Math.multiplyExact(secondsUntil(end), NANOS_PER_SECOND);
 953         return Math.addExact(secs, end.nanos - nanos);
 954     }
 955 
 956     private long secondsUntil(Instant end) {
 957         return Math.subtractExact(end.seconds, seconds);
 958     }
 959 
 960     //-----------------------------------------------------------------------
 961     /**
 962      * Converts this instant to the number of milliseconds from the epoch
 963      * of 1970-01-01T00:00:00Z.
 964      * <p>
 965      * If this instant represents a point on the time-line too far in the future
 966      * or past to fit in a {@code long} milliseconds, then an exception is thrown.
 967      * <p>
 968      * If this instant has greater than millisecond precision, then the conversion
 969      * will drop any excess precision information as though the amount in nanoseconds
 970      * was subject to integer division by one million.
 971      *
 972      * @return the number of milliseconds since the epoch of 1970-01-01T00:00:00Z
 973      * @throws ArithmeticException if numeric overflow occurs
 974      */
 975     public long toEpochMilli() {
 976         long millis = Math.multiplyExact(seconds, 1000);
 977         return millis + nanos / 1000_000;
 978     }
 979 
 980     //-----------------------------------------------------------------------
 981     /**
 982      * Compares this instant to the specified instant.
 983      * <p>
 984      * The comparison is based on the time-line position of the instants.
 985      * It is "consistent with equals", as defined by {@link Comparable}.
 986      *
 987      * @param otherInstant  the other instant to compare to, not null
 988      * @return the comparator value, negative if less, positive if greater
 989      * @throws NullPointerException if otherInstant is null
 990      */
 991     @Override
 992     public int compareTo(Instant otherInstant) {
 993         int cmp = Long.compare(seconds, otherInstant.seconds);
 994         if (cmp != 0) {
 995             return cmp;
 996         }
 997         return nanos - otherInstant.nanos;
 998     }
 999 
1000     /**
1001      * Checks if this instant is after the specified instant.
1002      * <p>
1003      * The comparison is based on the time-line position of the instants.
1004      *
1005      * @param otherInstant  the other instant to compare to, not null
1006      * @return true if this instant is after the specified instant
1007      * @throws NullPointerException if otherInstant is null
1008      */
1009     public boolean isAfter(Instant otherInstant) {
1010         return compareTo(otherInstant) > 0;
1011     }
1012 
1013     /**
1014      * Checks if this instant is before the specified instant.
1015      * <p>
1016      * The comparison is based on the time-line position of the instants.
1017      *
1018      * @param otherInstant  the other instant to compare to, not null
1019      * @return true if this instant is before the specified instant
1020      * @throws NullPointerException if otherInstant is null
1021      */
1022     public boolean isBefore(Instant otherInstant) {
1023         return compareTo(otherInstant) < 0;
1024     }
1025 
1026     //-----------------------------------------------------------------------
1027     /**
1028      * Checks if this instant is equal to the specified instant.
1029      * <p>
1030      * The comparison is based on the time-line position of the instants.
1031      *
1032      * @param otherInstant  the other instant, null returns false
1033      * @return true if the other instant is equal to this one
1034      */
1035     @Override
1036     public boolean equals(Object otherInstant) {
1037         if (this == otherInstant) {
1038             return true;
1039         }
1040         if (otherInstant instanceof Instant) {
1041             Instant other = (Instant) otherInstant;
1042             return this.seconds == other.seconds &&
1043                    this.nanos == other.nanos;
1044         }
1045         return false;
1046     }
1047 
1048     /**
1049      * Returns a hash code for this instant.
1050      *
1051      * @return a suitable hash code
1052      */
1053     @Override
1054     public int hashCode() {
1055         return ((int) (seconds ^ (seconds >>> 32))) + 51 * nanos;
1056     }
1057 
1058     //-----------------------------------------------------------------------
1059     /**
1060      * A string representation of this instant using ISO-8601 representation.
1061      * <p>
1062      * The format used is the same as {@link DateTimeFormatters#isoInstant()}.
1063      *
1064      * @return an ISO-8601 representation of this instant, not null
1065      */
1066     @Override
1067     public String toString() {
1068         return DateTimeFormatters.isoInstant().print(this);
1069     }
1070 
1071     // -----------------------------------------------------------------------
1072     /**
1073      * Writes the object using a
1074      * <a href="../../serialized-form.html#java.time.Ser">dedicated serialized form</a>.
1075      * <pre>
1076      *  out.writeByte(2);  // identifies this as an Instant
1077      *  out.writeLong(seconds);
1078      *  out.writeInt(nanos);
1079      * </pre>
1080      *
1081      * @return the instance of {@code Ser}, not null
1082      */
1083     private Object writeReplace() {
1084         return new Ser(Ser.INSTANT_TYPE, this);
1085     }
1086 
1087     /**
1088      * Defend against malicious streams.
1089      * @return never
1090      * @throws InvalidObjectException always
1091      */
1092     private Object readResolve() throws ObjectStreamException {
1093         throw new InvalidObjectException("Deserialization via serialization delegate");
1094     }
1095 
1096     void writeExternal(DataOutput out) throws IOException {
1097         out.writeLong(seconds);
1098         out.writeInt(nanos);
1099     }
1100 
1101     static Instant readExternal(DataInput in) throws IOException {
1102         long seconds = in.readLong();
1103         int nanos = in.readInt();
1104         return Instant.ofEpochSecond(seconds, nanos);
1105     }
1106 
1107 }