src/java.base/share/classes/java/time/Instant.java
author darcy
Thu, 29 Aug 2019 16:31:34 -0700
changeset 57956 e0b8b019d2f5
parent 54206 003cc64366da
permissions -rw-r--r--
8229997: Apply java.io.Serial annotations in java.base Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 52078
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     4
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    10
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    15
 * accompanied this code).
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    16
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    20
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    23
 * questions.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    24
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    25
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    26
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    30
 * file:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    31
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    32
 * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    33
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    34
 * All rights reserved.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    35
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    36
 * Redistribution and use in source and binary forms, with or without
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    37
 * modification, are permitted provided that the following conditions are met:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    38
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    39
 *  * Redistributions of source code must retain the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    40
 *    this list of conditions and the following disclaimer.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    41
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    42
 *  * Redistributions in binary form must reproduce the above copyright notice,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    43
 *    this list of conditions and the following disclaimer in the documentation
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    44
 *    and/or other materials provided with the distribution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    45
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    46
 *  * Neither the name of JSR-310 nor the names of its contributors
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    47
 *    may be used to endorse or promote products derived from this software
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    48
 *    without specific prior written permission.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    49
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    50
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    51
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    52
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    53
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    55
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    56
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    57
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    58
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    59
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    61
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    62
package java.time;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    63
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    64
import static java.time.LocalTime.NANOS_PER_SECOND;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    65
import static java.time.LocalTime.SECONDS_PER_DAY;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    66
import static java.time.LocalTime.SECONDS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    67
import static java.time.LocalTime.SECONDS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    68
import static java.time.temporal.ChronoField.INSTANT_SECONDS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    69
import static java.time.temporal.ChronoField.MICRO_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    70
import static java.time.temporal.ChronoField.MILLI_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    71
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
    72
import static java.time.temporal.ChronoUnit.DAYS;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    73
import static java.time.temporal.ChronoUnit.NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    74
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    75
import java.io.DataInput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    76
import java.io.DataOutput;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    77
import java.io.IOException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    78
import java.io.InvalidObjectException;
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20873
diff changeset
    79
import java.io.ObjectInputStream;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    80
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    81
import java.time.format.DateTimeFormatter;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    87
import java.time.temporal.TemporalAdjuster;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    88
import java.time.temporal.TemporalAmount;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    89
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
    90
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    92
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    93
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * An instantaneous point on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * This class models a single instantaneous point on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * This might be used to record event time-stamps in the application.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * The range of an instant requires the storage of a number larger than a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * To achieve this, the class stores a {@code long} representing epoch-seconds and an
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * {@code int} representing nanosecond-of-second, which will always be between 0 and 999,999,999.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * The epoch-seconds are measured from the standard Java epoch of {@code 1970-01-01T00:00:00Z}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * where instants after the epoch have positive values, and earlier instants have negative values.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   108
 * For both the epoch-second and nanosecond parts, a larger value is always later on the time-line
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   109
 * than a smaller value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   110
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 52078
diff changeset
   111
 * <h2>Time-scale</h2>
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   112
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   113
 * The length of the solar day is the standard way that humans measure time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 * This has traditionally been subdivided into 24 hours of 60 minutes of 60 seconds,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * forming a 86400 second day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   116
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   117
 * Modern timekeeping is based on atomic clocks which precisely define an SI second
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   118
 * relative to the transitions of a Caesium atom. The length of an SI second was defined
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   119
 * to be very close to the 86400th fraction of a day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   120
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   121
 * Unfortunately, as the Earth rotates the length of the day varies.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
 * In addition, over time the average length of the day is getting longer as the Earth slows.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   123
 * As a result, the length of a solar day in 2012 is slightly longer than 86400 SI seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * The actual length of any given day and the amount by which the Earth is slowing
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * are not predictable and can only be determined by measurement.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 * The UT1 time-scale captures the accurate length of day, but is only available some
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   127
 * time after the day has completed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   128
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   129
 * The UTC time-scale is a standard approach to bundle up all the additional fractions
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
 * of a second from UT1 into whole seconds, known as <i>leap-seconds</i>.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   131
 * A leap-second may be added or removed depending on the Earth's rotational changes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
 * As such, UTC permits a day to have 86399 SI seconds or 86401 SI seconds where
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
 * necessary in order to keep the day aligned with the Sun.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   134
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   135
 * The modern UTC time-scale was introduced in 1972, introducing the concept of whole leap-seconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   136
 * Between 1958 and 1972, the definition of UTC was complex, with minor sub-second leaps and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   137
 * alterations to the length of the notional second. As of 2012, discussions are underway
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
 * to change the definition of UTC again, with the potential to remove leap seconds or
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
 * introduce other changes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   140
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   141
 * Given the complexity of accurate timekeeping described above, this Java API defines
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   142
 * its own time-scale, the <i>Java Time-Scale</i>.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   143
 * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   144
 * The Java Time-Scale divides each calendar day into exactly 86400
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   145
 * subdivisions, known as seconds.  These seconds may differ from the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   146
 * SI second.  It closely matches the de facto international civil time
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   147
 * scale, the definition of which changes from time to time.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   148
 * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   149
 * The Java Time-Scale has slightly different definitions for different
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   150
 * segments of the time-line, each based on the consensus international
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   151
 * time scale that is used as the basis for civil time. Whenever the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   152
 * internationally-agreed time scale is modified or replaced, a new
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   153
 * segment of the Java Time-Scale must be defined for it.  Each segment
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   154
 * must meet these requirements:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   155
 * <ul>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   156
 * <li>the Java Time-Scale shall closely match the underlying international
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   157
 *  civil time scale;</li>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   158
 * <li>the Java Time-Scale shall exactly match the international civil
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   159
 *  time scale at noon each day;</li>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   160
 * <li>the Java Time-Scale shall have a precisely-defined relationship to
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   161
 *  the international civil time scale.</li>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   162
 * </ul>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   163
 * There are currently, as of 2013, two segments in the Java time-scale.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   164
 * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   165
 * For the segment from 1972-11-03 (exact boundary discussed below) until
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   166
 * further notice, the consensus international time scale is UTC (with
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   167
 * leap seconds).  In this segment, the Java Time-Scale is identical to
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   168
 * <a href="http://www.cl.cam.ac.uk/~mgk25/time/utc-sls/">UTC-SLS</a>.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   169
 * This is identical to UTC on days that do not have a leap second.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   170
 * On days that do have a leap second, the leap second is spread equally
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   171
 * over the last 1000 seconds of the day, maintaining the appearance of
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   172
 * exactly 86400 seconds per day.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   173
 * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   174
 * For the segment prior to 1972-11-03, extending back arbitrarily far,
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   175
 * the consensus international time scale is defined to be UT1, applied
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   176
 * proleptically, which is equivalent to the (mean) solar time on the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   177
 * prime meridian (Greenwich). In this segment, the Java Time-Scale is
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   178
 * identical to the consensus international time scale. The exact
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   179
 * boundary between the two segments is the instant where UT1 = UTC
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   180
 * between 1972-11-03T00:00 and 1972-11-04T12:00.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   181
 * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   182
 * Implementations of the Java time-scale using the JSR-310 API are not
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   183
 * required to provide any clock that is sub-second accurate, or that
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   184
 * progresses monotonically or smoothly. Implementations are therefore
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   185
 * not required to actually perform the UTC-SLS slew or to otherwise be
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   186
 * aware of leap seconds. JSR-310 does, however, require that
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   187
 * implementations must document the approach they use when defining a
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   188
 * clock representing the current instant.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   189
 * See {@link Clock} for details on the available clocks.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   190
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   191
 * The Java time-scale is used for all date-time classes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   192
 * This includes {@code Instant}, {@code LocalDate}, {@code LocalTime}, {@code OffsetDateTime},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   193
 * {@code ZonedDateTime} and {@code Duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
 *
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   195
 * <p>
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48739
diff changeset
   196
 * This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
22108
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   197
 * class; use of identity-sensitive operations (including reference equality
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   198
 * ({@code ==}), identity hash code, or synchronization) on instances of
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   199
 * {@code Instant} may have unpredictable results and should be avoided.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   200
 * The {@code equals} method should be used for comparisons.
99859c0e9a33 8029551: Add value-type notice to java.time classes
rriggs
parents: 22081
diff changeset
   201
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   202
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
public final class Instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
        implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
     * Constant for the 1970-01-01T00:00:00Z epoch instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
    public static final Instant EPOCH = new Instant(0, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
     * The minimum supported epoch second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
    private static final long MIN_SECOND = -31557014167219200L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
     * The maximum supported epoch second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
    private static final long MAX_SECOND = 31556889864403199L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * This could be used by an application as a "far past" instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * This is one year earlier than the minimum {@code LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   227
     * This provides sufficient values to handle the range of {@code ZoneOffset}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     * which affect the instant in addition to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
     * The value is also chosen such that the value of the year fits in
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
     * an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   231
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
    public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   234
     * The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * This could be used by an application as a "far future" instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
     * This is one year later than the maximum {@code LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   238
     * This provides sufficient values to handle the range of {@code ZoneOffset}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     * which affect the instant in addition to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
     * The value is also chosen such that the value of the year fits in
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
     * an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
    public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
   248
    @java.io.Serial
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
    private static final long serialVersionUID = -665713676816604388L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   251
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   252
     * The number of seconds from the epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
    private final long seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
     * The number of nanoseconds, later along the time-line, from the seconds field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
     * This is always positive, and never exceeds 999,999,999.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
    private final int nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * Obtains the current instant from the system clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   265
     * This will query the {@link Clock#systemUTC() system UTC clock} to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     * obtain the current instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     * Using this method will prevent the ability to use an alternate time-source for
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
     * testing because the clock is effectively hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
     * @return the current instant using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
    public static Instant now() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
        return Clock.systemUTC().instant();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
     * Obtains the current instant from the specified clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     * This will query the specified clock to obtain the current time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
     * Using this method allows the use of an alternate clock for testing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     * The alternate clock may be introduced using {@link Clock dependency injection}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
     * @param clock  the clock to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
     * @return the current instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
    public static Instant now(Clock clock) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
        Objects.requireNonNull(clock, "clock");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
        return clock.instant();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     * Obtains an instance of {@code Instant} using seconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * The nanosecond field is set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
     * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
     * @return an instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
    public static Instant ofEpochSecond(long epochSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
        return create(epochSecond, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * Obtains an instance of {@code Instant} using seconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * This method allows an arbitrary number of nanoseconds to be passed in.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   313
     * The factory will alter the values of the second and nanosecond in order
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   314
     * to ensure that the stored nanosecond is in the range 0 to 999,999,999.
50473
bef0ed237504 8204444: java.time cleanup
martin
parents: 49433
diff changeset
   315
     * For example, the following will result in exactly the same instant:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   317
     *  Instant.ofEpochSecond(3, 1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   318
     *  Instant.ofEpochSecond(4, -999_999_999);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   319
     *  Instant.ofEpochSecond(2, 1000_000_001);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     * @param nanoAdjustment  the nanosecond adjustment to the number of seconds, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
     * @return an instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
    public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
        long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
        int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
        return create(secs, nos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     * Obtains an instance of {@code Instant} using milliseconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * The seconds and nanoseconds are extracted from the specified milliseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
     * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
     * @return an instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
    public static Instant ofEpochMilli(long epochMilli) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
        long secs = Math.floorDiv(epochMilli, 1000);
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 37880
diff changeset
   346
        int mos = Math.floorMod(epochMilli, 1000);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
        return create(secs, mos * 1000_000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   350
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   351
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   352
     * Obtains an instance of {@code Instant} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   354
     * This obtains an instant based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   355
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   356
     * which this factory converts to an instance of {@code Instant}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   359
     * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * allowing it to be used as a query via method reference, {@code Instant::from}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   365
     * @return the instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   366
     * @throws DateTimeException if unable to convert to an {@code Instant}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   367
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   368
    public static Instant from(TemporalAccessor temporal) {
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   369
        if (temporal instanceof Instant) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   370
            return (Instant) temporal;
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   371
        }
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   372
        Objects.requireNonNull(temporal, "temporal");
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   373
        try {
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   374
            long instantSecs = temporal.getLong(INSTANT_SECONDS);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   375
            int nanoOfSecond = temporal.get(NANO_OF_SECOND);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   376
            return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   377
        } catch (DateTimeException ex) {
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   378
            throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
24986
81e3c2e485d4 8046416: Unable to parse an Instant from fields
scolebourne
parents: 24985
diff changeset
   379
                    temporal + " of type " + temporal.getClass().getName(), ex);
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   380
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   382
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   385
     * Obtains an instance of {@code Instant} from a text string such as
20521
0a672997e442 8024999: Instant.Parse typo in example
rriggs
parents: 20520
diff changeset
   386
     * {@code 2007-12-03T10:15:30.00Z}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * The string must represent a valid instant in UTC and is parsed using
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   389
     * {@link DateTimeFormatter#ISO_INSTANT}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   392
     * @return the parsed instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
    public static Instant parse(final CharSequence text) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   396
        return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * Obtains an instance of {@code Instant} using seconds and nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
     * @param seconds  the length of the duration in seconds
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
     * @param nanoOfSecond  the nano-of-second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
    private static Instant create(long seconds, int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
        if ((seconds | nanoOfSecond) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
            return EPOCH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
        if (seconds < MIN_SECOND || seconds > MAX_SECOND) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
            throw new DateTimeException("Instant exceeds minimum or maximum instant");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
        return new Instant(seconds, nanoOfSecond);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * Constructs an instance of {@code Instant} using seconds from the epoch of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     * 1970-01-01T00:00:00Z and nanosecond fraction of second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
     * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
     * @param nanos  the nanoseconds within the second, must be positive
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
    private Instant(long epochSecond, int nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
        super();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
        this.seconds = epochSecond;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
        this.nanos = nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   431
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   432
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   433
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * This checks if this instant can be queried for the specified field.
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   435
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   436
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   437
     * methods will throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     * The supported fields are:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * <li>{@code NANO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     * <li>{@code MICRO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
     * <li>{@code MILLI_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * <li>{@code INSTANT_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   446
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   450
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
     * @return true if the field is supported on this instant, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   457
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   458
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
            return field == INSTANT_SECONDS || field == NANO_OF_SECOND || field == MICRO_OF_SECOND || field == MILLI_OF_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   462
        return field != null && field.isSupportedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   463
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   464
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   465
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   466
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   467
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   468
     * This checks if the specified unit can be added to, or subtracted from, this date-time.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   469
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   470
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   471
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   472
     * If the unit is a {@link ChronoUnit} then the query is implemented here.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   473
     * The supported units are:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   474
     * <ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   475
     * <li>{@code NANOS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   476
     * <li>{@code MICROS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   477
     * <li>{@code MILLIS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   478
     * <li>{@code SECONDS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   479
     * <li>{@code MINUTES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   480
     * <li>{@code HOURS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   481
     * <li>{@code HALF_DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   482
     * <li>{@code DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   483
     * </ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   484
     * All other {@code ChronoUnit} instances will return false.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   485
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   486
     * If the unit is not a {@code ChronoUnit}, then the result of this method
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   487
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   488
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   489
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   490
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   491
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   492
     * @return true if the unit can be added/subtracted, false if not
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   493
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   494
    @Override
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   495
    public boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   496
        if (unit instanceof ChronoUnit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   497
            return unit.isTimeBased() || unit == DAYS;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   498
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   499
        return unit != null && unit.isSupportedBy(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   500
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   501
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   502
    //-----------------------------------------------------------------------
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   503
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   505
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
     * This instant is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     * If it is not possible to return the range, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   510
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
     * The {@link #isSupported(TemporalField) supported fields} will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   513
     * appropriate range instances.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   514
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   517
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   520
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
     * @throws DateTimeException if the range for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   524
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
        return Temporal.super.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   529
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   532
     * Gets the value of the specified field from this instant as an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   533
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22654
diff changeset
   534
     * This queries this instant for the value of the specified field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
     * The returned value will always be within the valid range of values for the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   539
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
     * values based on this date-time, except {@code INSTANT_SECONDS} which is too
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   542
     * large to fit in an {@code int} and throws a {@code DateTimeException}.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   543
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   546
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   548
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   549
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   550
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   551
     * @return the value for the field
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   552
     * @throws DateTimeException if a value for the field cannot be obtained or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   553
     *         the value is outside the range of valid values for the field
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   554
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   555
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
            switch ((ChronoField) field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
                case NANO_OF_SECOND: return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   563
                case MICRO_OF_SECOND: return nanos / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
                case MILLI_OF_SECOND: return nanos / 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   565
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   566
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   568
        return range(field).checkValidIntValue(field.getFrom(this), field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   569
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   570
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
     * Gets the value of the specified field from this instant as a {@code long}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   573
     * <p>
24256
da9a41004459 8034906: Fix typos, errors and Javadoc differences in java.time
scolebourne
parents: 22654
diff changeset
   574
     * This queries this instant for the value of the specified field.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
     * If it is not possible to return the value, because the field is not supported
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   578
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
     * values based on this date-time.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   581
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   584
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   588
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
     * @throws DateTimeException if a value for the field cannot be obtained
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   591
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
            switch ((ChronoField) field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
                case NANO_OF_SECOND: return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
                case MICRO_OF_SECOND: return nanos / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   600
                case MILLI_OF_SECOND: return nanos / 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
                case INSTANT_SECONDS: return seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   602
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   603
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   605
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   607
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   608
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
     * Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
     * The epoch second count is a simple incrementing count of seconds where
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
     * second 0 is 1970-01-01T00:00:00Z.
48739
59bba47f435e 8196609: Improve javadoc for java.time.Instant.getEpochSecond
martin
parents: 47216
diff changeset
   614
     * The nanosecond part is returned by {@link #getNano}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   615
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
     * @return the seconds from the epoch of 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
    public long getEpochSecond() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
        return seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
     * Gets the number of nanoseconds, later along the time-line, from the start
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
     * of the second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     * The nanosecond-of-second value measures the total number of nanoseconds from
41492
07554e9682b7 8167166: Java API docs mention a non-existent method getNanosOfSecond
rriggs
parents: 39343
diff changeset
   627
     * the second returned by {@link #getEpochSecond}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
     * @return the nanoseconds within the second, always positive, never exceeds 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
    public int getNano() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
        return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   636
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   637
     * Returns an adjusted copy of this instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   639
     * This returns an {@code Instant}, based on this one, with the instant adjusted.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
     * The adjustment takes place using the specified adjuster strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
     * Read the documentation of the adjuster to understand what adjustment will be made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
     * specified adjuster passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   648
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
     * @param adjuster the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
     * @return an {@code Instant} based on {@code this} with the adjustment made, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
     * @throws DateTimeException if the adjustment cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
    public Instant with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
        return (Instant) adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   658
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   659
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
     * Returns a copy of this instant with the specified field set to a new value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   662
     * This returns an {@code Instant}, based on this one, with the value
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     * for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     * If it is not possible to set the value, because the field is not supported or for
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     * The supported fields behave as follows:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
     * <li>{@code NANO_OF_SECOND} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
     *  Returns an {@code Instant} with the specified nano-of-second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
     *  The epoch-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
     * <li>{@code MICRO_OF_SECOND} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   674
     *  Returns an {@code Instant} with the nano-of-second replaced by the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   675
     *  micro-of-second multiplied by 1,000. The epoch-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   676
     * <li>{@code MILLI_OF_SECOND} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
     *  Returns an {@code Instant} with the nano-of-second replaced by the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
     *  milli-of-second multiplied by 1,000,000. The epoch-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
     * <li>{@code INSTANT_SECONDS} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
     *  Returns an {@code Instant} with the specified epoch-second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
     *  The nano-of-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   682
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   684
     * In all cases, if the new value is outside the valid range of values for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
     * then a {@code DateTimeException} will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   687
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     * If the field is not a {@code ChronoField}, then the result of this method
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   690
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
     * passing {@code this} as the argument. In this case, the field determines
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     * whether and how to adjust the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   696
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   697
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
     * @return an {@code Instant} based on {@code this} with the specified field set, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   700
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
    public Instant with(TemporalField field, long newValue) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
            ChronoField f = (ChronoField) field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
            f.checkValidValue(newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
                case MILLI_OF_SECOND: {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
                    int nval = (int) newValue * 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
                    return (nval != nanos ? create(seconds, nval) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
                case MICRO_OF_SECOND: {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
                    int nval = (int) newValue * 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
                    return (nval != nanos ? create(seconds, nval) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   717
                case NANO_OF_SECOND: return (newValue != nanos ? create(seconds, (int) newValue) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
                case INSTANT_SECONDS: return (newValue != seconds ? create(newValue, nanos) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   719
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   720
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   721
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   722
        return field.adjustInto(this, newValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   723
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   724
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   725
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   726
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   727
     * Returns a copy of this {@code Instant} truncated to the specified unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   728
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   729
     * Truncating the instant returns a copy of the original with fields
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   730
     * smaller than the specified unit set to zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   731
     * The fields are calculated on the basis of using a UTC offset as seen
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   732
     * in {@code toString}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   733
     * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   734
     * round down to the nearest minute, setting the seconds and nanoseconds to zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   735
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   736
     * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   737
     * that divides into the length of a standard day without remainder.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   738
     * This includes all supplied time units on {@link ChronoUnit} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   739
     * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   740
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   741
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   742
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   743
     * @param unit  the unit to truncate to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   744
     * @return an {@code Instant} based on this instant with the time truncated, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   745
     * @throws DateTimeException if the unit is invalid for truncation
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   746
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   747
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   748
    public Instant truncatedTo(TemporalUnit unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   749
        if (unit == ChronoUnit.NANOS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   750
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   751
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   752
        Duration unitDur = unit.getDuration();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   753
        if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   754
            throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   755
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   756
        long dur = unitDur.toNanos();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   757
        if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   758
            throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   759
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   760
        long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
50473
bef0ed237504 8204444: java.time cleanup
martin
parents: 49433
diff changeset
   761
        long result = Math.floorDiv(nod, dur) * dur;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   762
        return plusNanos(result - nod);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   764
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   765
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   766
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   767
     * Returns a copy of this instant with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   768
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   769
     * This returns an {@code Instant}, based on this one, with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   770
     * The amount is typically {@link Duration} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   771
     * the {@link TemporalAmount} interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   772
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   773
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   774
     * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   775
     * to implement the addition in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   776
     * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   777
     * of the amount implementation to determine if it can be successfully added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   778
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   779
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   780
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   781
     * @param amountToAdd  the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   782
     * @return an {@code Instant} based on this instant with the addition made, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   783
     * @throws DateTimeException if the addition cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   784
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   785
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   787
    public Instant plus(TemporalAmount amountToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   788
        return (Instant) amountToAdd.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   789
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   790
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   791
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   792
     * Returns a copy of this instant with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   793
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   794
     * This returns an {@code Instant}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   795
     * in terms of the unit added. If it is not possible to add the amount, because the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   796
     * unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   797
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   798
     * If the field is a {@link ChronoUnit} then the addition is implemented here.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   799
     * The supported fields behave as follows:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   800
     * <ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   801
     * <li>{@code NANOS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   802
     *  Returns an {@code Instant} with the specified number of nanoseconds added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   803
     *  This is equivalent to {@link #plusNanos(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   804
     * <li>{@code MICROS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   805
     *  Returns an {@code Instant} with the specified number of microseconds added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   806
     *  This is equivalent to {@link #plusNanos(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   807
     *  multiplied by 1,000.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   808
     * <li>{@code MILLIS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   809
     *  Returns an {@code Instant} with the specified number of milliseconds added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   810
     *  This is equivalent to {@link #plusNanos(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   811
     *  multiplied by 1,000,000.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   812
     * <li>{@code SECONDS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   813
     *  Returns an {@code Instant} with the specified number of seconds added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   814
     *  This is equivalent to {@link #plusSeconds(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   815
     * <li>{@code MINUTES} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   816
     *  Returns an {@code Instant} with the specified number of minutes added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   817
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   818
     *  multiplied by 60.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   819
     * <li>{@code HOURS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   820
     *  Returns an {@code Instant} with the specified number of hours added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   821
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   822
     *  multiplied by 3,600.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   823
     * <li>{@code HALF_DAYS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   824
     *  Returns an {@code Instant} with the specified number of half-days added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   825
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   826
     *  multiplied by 43,200 (12 hours).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   827
     * <li>{@code DAYS} -
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   828
     *  Returns an {@code Instant} with the specified number of days added.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   829
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   830
     *  multiplied by 86,400 (24 hours).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   831
     * </ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   832
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   833
     * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   834
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   835
     * If the field is not a {@code ChronoUnit}, then the result of this method
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   836
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   837
     * passing {@code this} as the argument. In this case, the unit determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   838
     * whether and how to perform the addition.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   839
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   840
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   841
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   842
     * @param amountToAdd  the amount of the unit to add to the result, may be negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   843
     * @param unit  the unit of the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   844
     * @return an {@code Instant} based on this instant with the specified amount added, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   845
     * @throws DateTimeException if the addition cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   846
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   847
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   850
    public Instant plus(long amountToAdd, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
            switch ((ChronoUnit) unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
                case NANOS: return plusNanos(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
                case MICROS: return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
                case MILLIS: return plusMillis(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
                case SECONDS: return plusSeconds(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
                case MINUTES: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
                case HOURS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   859
                case HALF_DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
                case DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   861
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   862
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   864
        return unit.addTo(this, amountToAdd);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
     * Returns a copy of this instant with the specified duration in seconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   872
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
     * @param secondsToAdd  the seconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
     * @return an {@code Instant} based on this instant with the specified seconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   876
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
    public Instant plusSeconds(long secondsToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
        return plus(secondsToAdd, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
     * Returns a copy of this instant with the specified duration in milliseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   886
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
     * @param millisToAdd  the milliseconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
     * @return an {@code Instant} based on this instant with the specified milliseconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
    public Instant plusMillis(long millisToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
        return plus(millisToAdd / 1000, (millisToAdd % 1000) * 1000_000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     * Returns a copy of this instant with the specified duration in nanoseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   900
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
     * @param nanosToAdd  the nanoseconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
     * @return an {@code Instant} based on this instant with the specified nanoseconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
    public Instant plusNanos(long nanosToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
        return plus(0, nanosToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
     * Returns a copy of this instant with the specified duration added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   915
     * @param secondsToAdd  the seconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     * @param nanosToAdd  the nanos to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
     * @return an {@code Instant} based on this instant with the specified seconds added, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
    private Instant plus(long secondsToAdd, long nanosToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
        if ((secondsToAdd | nanosToAdd) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
        long epochSec = Math.addExact(seconds, secondsToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
        epochSec = Math.addExact(epochSec, nanosToAdd / NANOS_PER_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
        nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
        long nanoAdjustment = nanos + nanosToAdd;  // safe int+NANOS_PER_SECOND
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
        return ofEpochSecond(epochSec, nanoAdjustment);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   931
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   932
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   933
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   934
     * Returns a copy of this instant with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   935
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   936
     * This returns an {@code Instant}, based on this one, with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   937
     * The amount is typically {@link Duration} but may be any other type implementing
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   938
     * the {@link TemporalAmount} interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   939
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   940
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   941
     * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   942
     * to implement the subtraction in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   943
     * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   944
     * of the amount implementation to determine if it can be successfully subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   945
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   946
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   947
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   948
     * @param amountToSubtract  the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   949
     * @return an {@code Instant} based on this instant with the subtraction made, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   950
     * @throws DateTimeException if the subtraction cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   951
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   952
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   954
    public Instant minus(TemporalAmount amountToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   955
        return (Instant) amountToSubtract.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   956
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   957
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   958
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   959
     * Returns a copy of this instant with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   960
     * <p>
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 36662
diff changeset
   961
     * This returns an {@code Instant}, based on this one, with the amount
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   962
     * in terms of the unit subtracted. If it is not possible to subtract the amount,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   963
     * because the unit is not supported or for some other reason, an exception is thrown.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   964
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   965
     * This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   966
     * See that method for a full description of how addition, and thus subtraction, works.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   967
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   968
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   969
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   970
     * @param amountToSubtract  the amount of the unit to subtract from the result, may be negative
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   971
     * @param unit  the unit of the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   972
     * @return an {@code Instant} based on this instant with the specified amount subtracted, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   973
     * @throws DateTimeException if the subtraction cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   974
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   975
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   977
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
    public Instant minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
        return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
     * Returns a copy of this instant with the specified duration in seconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   987
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     * @param secondsToSubtract  the seconds to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
     * @return an {@code Instant} based on this instant with the specified seconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
    public Instant minusSeconds(long secondsToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
        if (secondsToSubtract == Long.MIN_VALUE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
            return plusSeconds(Long.MAX_VALUE).plusSeconds(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
        return plusSeconds(-secondsToSubtract);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   998
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
     * Returns a copy of this instant with the specified duration in milliseconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1004
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     * @param millisToSubtract  the milliseconds to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
     * @return an {@code Instant} based on this instant with the specified milliseconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
    public Instant minusMillis(long millisToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
        if (millisToSubtract == Long.MIN_VALUE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
            return plusMillis(Long.MAX_VALUE).plusMillis(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
        return plusMillis(-millisToSubtract);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1018
     * Returns a copy of this instant with the specified duration in nanoseconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1021
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
     * @param nanosToSubtract  the nanoseconds to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
     * @return an {@code Instant} based on this instant with the specified nanoseconds subtracted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
    public Instant minusNanos(long nanosToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
        if (nanosToSubtract == Long.MIN_VALUE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
            return plusNanos(Long.MAX_VALUE).plusNanos(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
        return plusNanos(-nanosToSubtract);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
     * Queries this instant using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
     * This queries this instant using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1042
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1053
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1054
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
    public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1056
        if (query == TemporalQueries.precision()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1057
            return (R) NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1058
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1059
        // inline TemporalAccessor.super.query(query) as an optimization
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1060
        if (query == TemporalQueries.chronology() || query == TemporalQueries.zoneId() ||
24985
61ec15d123c3 8046707: Performance of java.time could be better
scolebourne
parents: 24256
diff changeset
  1061
                query == TemporalQueries.zone() || query == TemporalQueries.offset() ||
61ec15d123c3 8046707: Performance of java.time could be better
scolebourne
parents: 24256
diff changeset
  1062
                query == TemporalQueries.localDate() || query == TemporalQueries.localTime()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
            return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
        return query.queryFrom(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
     * Adjusts the specified temporal object to have this instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
     * with the instant changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1073
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1074
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
     * twice, passing {@link ChronoField#INSTANT_SECONDS} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
     * {@link ChronoField#NANO_OF_SECOND} as the fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1080
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1081
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
     *   temporal = thisInstant.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
     *   temporal = temporal.with(thisInstant);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1090
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1094
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1095
        return temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1096
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1097
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1098
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1099
     * Calculates the amount of time until another instant in terms of the specified unit.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1101
     * This calculates the amount of time between two {@code Instant}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1102
     * objects in terms of a single {@code TemporalUnit}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1103
     * The start and end points are {@code this} and the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1104
     * The result will be negative if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1105
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1106
     * complete units between the two instants.
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1107
     * The {@code Temporal} passed to this method is converted to a
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1108
     * {@code Instant} using {@link #from(TemporalAccessor)}.
36662
616fd89e60ac 8151868: Typo in java.time.Instant until(Temporal endExclusive, TemporalUnit unit)
rriggs
parents: 33243
diff changeset
  1109
     * For example, the amount in seconds between two dates can be calculated
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1110
     * using {@code startInstant.until(endInstant, SECONDS)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1112
     * There are two equivalent ways of using this method.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1113
     * The first is to invoke this method.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1114
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1115
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1116
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1117
     *   amount = start.until(end, SECONDS);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1118
     *   amount = SECONDS.between(start, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
     * </pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1120
     * The choice should be made based on which makes the code more readable.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1121
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1122
     * The calculation is implemented in this method for {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
     * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1124
     * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1125
     * are supported. Other {@code ChronoUnit} values will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1126
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
     * If the unit is not a {@code ChronoUnit}, then the result of this method
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1129
     * passing {@code this} as the first argument and the converted input temporal
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1130
     * as the second argument.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1131
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1132
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1133
     *
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1134
     * @param endExclusive  the end date, exclusive, which is converted to an {@code Instant}, not null
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1135
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1136
     * @return the amount of time between this instant and the end instant
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1137
     * @throws DateTimeException if the amount cannot be calculated, or the end
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1138
     *  temporal cannot be converted to an {@code Instant}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1139
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1140
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1142
    @Override
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1143
    public long until(Temporal endExclusive, TemporalUnit unit) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1144
        Instant end = Instant.from(endExclusive);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1145
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1146
            ChronoUnit f = (ChronoUnit) unit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1148
                case NANOS: return nanosUntil(end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1149
                case MICROS: return nanosUntil(end) / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1150
                case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1151
                case SECONDS: return secondsUntil(end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1152
                case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1153
                case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1154
                case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1155
                case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1156
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1157
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1159
        return unit.between(this, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1160
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1161
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1162
    private long nanosUntil(Instant end) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1163
        long secsDiff = Math.subtractExact(end.seconds, seconds);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1164
        long totalNanos = Math.multiplyExact(secsDiff, NANOS_PER_SECOND);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1165
        return Math.addExact(totalNanos, end.nanos - nanos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1166
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1167
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1168
    private long secondsUntil(Instant end) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1169
        long secsDiff = Math.subtractExact(end.seconds, seconds);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1170
        long nanosDiff = end.nanos - nanos;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1171
        if (secsDiff > 0 && nanosDiff < 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1172
            secsDiff--;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1173
        } else if (secsDiff < 0 && nanosDiff > 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1174
            secsDiff++;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1175
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1176
        return secsDiff;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1177
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1178
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1179
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1180
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1181
     * Combines this instant with an offset to create an {@code OffsetDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1182
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1183
     * This returns an {@code OffsetDateTime} formed from this instant at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1184
     * specified offset from UTC/Greenwich. An exception will be thrown if the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1185
     * instant is too large to fit into an offset date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1186
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1187
     * This method is equivalent to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1188
     * {@link OffsetDateTime#ofInstant(Instant, ZoneId) OffsetDateTime.ofInstant(this, offset)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1189
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1190
     * @param offset  the offset to combine with, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1191
     * @return the offset date-time formed from this instant and the specified offset, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1192
     * @throws DateTimeException if the result exceeds the supported range
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1193
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1194
    public OffsetDateTime atOffset(ZoneOffset offset) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1195
        return OffsetDateTime.ofInstant(this, offset);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1196
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1197
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1198
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1199
     * Combines this instant with a time-zone to create a {@code ZonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1200
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1201
     * This returns an {@code ZonedDateTime} formed from this instant at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1202
     * specified time-zone. An exception will be thrown if the instant is too
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1203
     * large to fit into a zoned date-time.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1204
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1205
     * This method is equivalent to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1206
     * {@link ZonedDateTime#ofInstant(Instant, ZoneId) ZonedDateTime.ofInstant(this, zone)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1207
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1208
     * @param zone  the zone to combine with, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1209
     * @return the zoned date-time formed from this instant and the specified zone, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1210
     * @throws DateTimeException if the result exceeds the supported range
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1211
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1212
    public ZonedDateTime atZone(ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1213
        return ZonedDateTime.ofInstant(this, zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1214
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1215
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1216
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1217
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1218
     * Converts this instant to the number of milliseconds from the epoch
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1219
     * of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1220
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1221
     * If this instant represents a point on the time-line too far in the future
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1222
     * or past to fit in a {@code long} milliseconds, then an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1223
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1224
     * If this instant has greater than millisecond precision, then the conversion
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1225
     * will drop any excess precision information as though the amount in nanoseconds
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1226
     * was subject to integer division by one million.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1227
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1228
     * @return the number of milliseconds since the epoch of 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1229
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1230
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1231
    public long toEpochMilli() {
29230
19c7760bc203 8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
dfuchs
parents: 25859
diff changeset
  1232
        if (seconds < 0 && nanos > 0) {
19c7760bc203 8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
dfuchs
parents: 25859
diff changeset
  1233
            long millis = Math.multiplyExact(seconds+1, 1000);
19c7760bc203 8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
dfuchs
parents: 25859
diff changeset
  1234
            long adjustment = nanos / 1000_000 - 1000;
32041
a7acd5afbd27 8133022: Instant.toEpochMilli() silently overflows
rriggs
parents: 29230
diff changeset
  1235
            return Math.addExact(millis, adjustment);
29230
19c7760bc203 8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
dfuchs
parents: 25859
diff changeset
  1236
        } else {
19c7760bc203 8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
dfuchs
parents: 25859
diff changeset
  1237
            long millis = Math.multiplyExact(seconds, 1000);
32041
a7acd5afbd27 8133022: Instant.toEpochMilli() silently overflows
rriggs
parents: 29230
diff changeset
  1238
            return Math.addExact(millis, nanos / 1000_000);
29230
19c7760bc203 8074032: Instant.ofEpochMilli(millis).toEpochMilli() can throw arithmetic overflow in toEpochMilli()
dfuchs
parents: 25859
diff changeset
  1239
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1240
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1241
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1242
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
     * Compares this instant to the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1245
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1246
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1247
     * It is "consistent with equals", as defined by {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1248
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1249
     * @param otherInstant  the other instant to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1250
     * @return the comparator value, negative if less, positive if greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1251
     * @throws NullPointerException if otherInstant is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1252
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1253
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1254
    public int compareTo(Instant otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1255
        int cmp = Long.compare(seconds, otherInstant.seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1256
        if (cmp != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
            return cmp;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1259
        return nanos - otherInstant.nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1260
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1261
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1262
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1263
     * Checks if this instant is after the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1264
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1265
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1266
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1267
     * @param otherInstant  the other instant to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1268
     * @return true if this instant is after the specified instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1269
     * @throws NullPointerException if otherInstant is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1270
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1271
    public boolean isAfter(Instant otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1272
        return compareTo(otherInstant) > 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1273
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1274
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1275
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1276
     * Checks if this instant is before the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1277
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1278
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1279
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1280
     * @param otherInstant  the other instant to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1281
     * @return true if this instant is before the specified instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1282
     * @throws NullPointerException if otherInstant is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1283
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1284
    public boolean isBefore(Instant otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1285
        return compareTo(otherInstant) < 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1286
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1287
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1288
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1289
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1290
     * Checks if this instant is equal to the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1291
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1292
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1293
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1294
     * @param otherInstant  the other instant, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1295
     * @return true if the other instant is equal to this one
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1296
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1297
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1298
    public boolean equals(Object otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1299
        if (this == otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1300
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1301
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1302
        if (otherInstant instanceof Instant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1303
            Instant other = (Instant) otherInstant;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1304
            return this.seconds == other.seconds &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1305
                   this.nanos == other.nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1306
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1307
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1308
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1309
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1310
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1311
     * Returns a hash code for this instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1312
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1313
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1314
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1315
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1316
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1317
        return ((int) (seconds ^ (seconds >>> 32))) + 51 * nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1318
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1319
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1320
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1321
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1322
     * A string representation of this instant using ISO-8601 representation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1323
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1324
     * The format used is the same as {@link DateTimeFormatter#ISO_INSTANT}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1325
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1326
     * @return an ISO-8601 representation of this instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1327
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1328
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1329
    public String toString() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1330
        return DateTimeFormatter.ISO_INSTANT.format(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1331
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1332
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1333
    // -----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1334
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1335
     * Writes the object using a
52078
4a63197816ce 8211952: Broken links in java.time API
jjg
parents: 50473
diff changeset
  1336
     * <a href="{@docRoot}/serialized-form.html#java.time.Ser">dedicated serialized form</a>.
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1337
     * @serialData
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1338
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1339
     *  out.writeByte(2);  // identifies an Instant
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1340
     *  out.writeLong(seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1341
     *  out.writeInt(nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1342
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1343
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1344
     * @return the instance of {@code Ser}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1345
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
  1346
    @java.io.Serial
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1347
    private Object writeReplace() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1348
        return new Ser(Ser.INSTANT_TYPE, this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1349
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1350
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1351
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1352
     * Defend against malicious streams.
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20873
diff changeset
  1353
     *
22566
4ebe53dd7814 8032502: java.time add @param tags to readObject
rriggs
parents: 22108
diff changeset
  1354
     * @param s the stream to read
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1355
     * @throws InvalidObjectException always
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1356
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54206
diff changeset
  1357
    @java.io.Serial
22081
86eb26ff8f2b 8030002: Enhance deserialization using readObject
rriggs
parents: 20873
diff changeset
  1358
    private void readObject(ObjectInputStream s) throws InvalidObjectException {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1359
        throw new InvalidObjectException("Deserialization via serialization delegate");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1360
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1361
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1362
    void writeExternal(DataOutput out) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1363
        out.writeLong(seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1364
        out.writeInt(nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1365
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1366
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1367
    static Instant readExternal(DataInput in) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1368
        long seconds = in.readLong();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1369
        int nanos = in.readInt();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1370
        return Instant.ofEpochSecond(seconds, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1371
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1372
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1373
}