jdk/src/share/classes/java/time/Instant.java
author rriggs
Thu, 17 Oct 2013 10:37:23 -0400
changeset 20873 e91d5b1cb8e6
parent 20795 8ec9e5b79828
child 22081 86eb26ff8f2b
permissions -rw-r--r--
8026516: javadoc errors in java.time Summary: Corrected links to TemporalQuery and TemporalField.resolve Reviewed-by: mduigou, darcy, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     1
/*
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
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;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    79
import java.io.Serializable;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    80
import java.time.format.DateTimeFormatter;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    81
import java.time.format.DateTimeParseException;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    82
import java.time.temporal.ChronoField;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    83
import java.time.temporal.ChronoUnit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    84
import java.time.temporal.Temporal;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    85
import java.time.temporal.TemporalAccessor;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    86
import java.time.temporal.TemporalAdjuster;
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
    87
import java.time.temporal.TemporalAmount;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    88
import java.time.temporal.TemporalField;
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
    89
import java.time.temporal.TemporalQueries;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    90
import java.time.temporal.TemporalQuery;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    91
import java.time.temporal.TemporalUnit;
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
    92
import java.time.temporal.UnsupportedTemporalTypeException;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    93
import java.time.temporal.ValueRange;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    94
import java.util.Objects;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    95
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    96
/**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    97
 * An instantaneous point on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    98
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
    99
 * This class models a single instantaneous point on the time-line.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   100
 * This might be used to record event time-stamps in the application.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   101
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   102
 * For practicality, the instant is stored with some constraints.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   103
 * The measurable time-line is restricted to the number of seconds that can be held
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   104
 * in a {@code long}. This is greater than the current estimated age of the universe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   105
 * The instant is stored to nanosecond resolution.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   106
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   107
 * 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
   108
 * 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
   109
 * {@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
   110
 * 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
   111
 * 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
   112
 * 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
   113
 * than a smaller value.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   114
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   115
 * <h3>Time-scale</h3>
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
 * 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
   118
 * 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
   119
 * forming a 86400 second 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
 * Modern timekeeping is based on atomic clocks which precisely define an SI second
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   122
 * 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
   123
 * to be very close to the 86400th fraction of a day.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   124
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   125
 * Unfortunately, as the Earth rotates the length of the day varies.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   126
 * 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
   127
 * 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
   128
 * 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
   129
 * are not predictable and can only be determined by measurement.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   130
 * 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
   131
 * time after the day has completed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   132
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   133
 * 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
   134
 * 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
   135
 * 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
   136
 * 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
   137
 * necessary in order to keep the day aligned with the Sun.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   138
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   139
 * 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
   140
 * 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
   141
 * 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
   142
 * 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
   143
 * introduce other changes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   144
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   145
 * 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
   146
 * its own time-scale, the <i>Java Time-Scale</i>.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   147
 * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   148
 * The Java Time-Scale divides each calendar day into exactly 86400
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   149
 * subdivisions, known as seconds.  These seconds may differ from the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   150
 * SI second.  It closely matches the de facto international civil time
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   151
 * scale, the definition of which changes from time to time.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   152
 * <p>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   153
 * The Java Time-Scale has slightly different definitions for different
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   154
 * segments of the time-line, each based on the consensus international
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   155
 * 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
   156
 * internationally-agreed time scale is modified or replaced, a new
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   157
 * 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
   158
 * must meet these requirements:
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   159
 * <ul>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   160
 * <li>the Java Time-Scale shall closely match the underlying international
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   161
 *  civil time scale;</li>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   162
 * <li>the Java Time-Scale shall exactly match the international civil
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   163
 *  time scale at noon each day;</li>
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   164
 * <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
   165
 *  the international civil time scale.</li>
20873
e91d5b1cb8e6 8026516: javadoc errors in java.time
rriggs
parents: 20795
diff changeset
   166
 * </ul>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   167
 * 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
   168
 * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   169
 * 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
   170
 * further notice, the consensus international time scale is UTC (with
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   171
 * 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
   172
 * <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
   173
 * 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
   174
 * 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
   175
 * 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
   176
 * exactly 86400 seconds per day.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   177
 * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   178
 * 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
   179
 * the consensus international time scale is defined to be UT1, applied
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   180
 * proleptically, which is equivalent to the (mean) solar time on the
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   181
 * prime meridian (Greenwich). In this segment, the Java Time-Scale is
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   182
 * identical to the consensus international time scale. The exact
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   183
 * boundary between the two segments is the instant where UT1 = UTC
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   184
 * between 1972-11-03T00:00 and 1972-11-04T12:00.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   185
 * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   186
 * 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
   187
 * 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
   188
 * progresses monotonically or smoothly. Implementations are therefore
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   189
 * 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
   190
 * aware of leap seconds. JSR-310 does, however, require that
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   191
 * implementations must document the approach they use when defining a
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   192
 * clock representing the current instant.
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   193
 * See {@link Clock} for details on the available clocks.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   194
 * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   195
 * The Java time-scale is used for all date-time classes.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   196
 * This includes {@code Instant}, {@code LocalDate}, {@code LocalTime}, {@code OffsetDateTime},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   197
 * {@code ZonedDateTime} and {@code Duration}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   198
 *
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
   199
 * @implSpec
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   200
 * This class is immutable and thread-safe.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   201
 *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   202
 * @since 1.8
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   203
 */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   204
public final class Instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   205
        implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   206
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   207
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   208
     * Constant for the 1970-01-01T00:00:00Z epoch instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   209
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   210
    public static final Instant EPOCH = new Instant(0, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   211
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   212
     * The minimum supported epoch second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   213
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   214
    private static final long MIN_SECOND = -31557014167219200L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   215
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   216
     * The maximum supported epoch second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   217
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   218
    private static final long MAX_SECOND = 31556889864403199L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   219
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   220
     * The minimum supported {@code Instant}, '-1000000000-01-01T00:00Z'.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   221
     * This could be used by an application as a "far past" instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   222
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   223
     * This is one year earlier than the minimum {@code LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   224
     * This provides sufficient values to handle the range of {@code ZoneOffset}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   225
     * which affect the instant in addition to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   226
     * 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
   227
     * an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   228
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   229
    public static final Instant MIN = Instant.ofEpochSecond(MIN_SECOND, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   230
    /**
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   231
     * The maximum supported {@code Instant}, '1000000000-12-31T23:59:59.999999999Z'.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   232
     * This could be used by an application as a "far future" instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   233
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   234
     * This is one year later than the maximum {@code LocalDateTime}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   235
     * This provides sufficient values to handle the range of {@code ZoneOffset}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   236
     * which affect the instant in addition to the local date-time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   237
     * 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
   238
     * an {@code int}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   239
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   240
    public static final Instant MAX = Instant.ofEpochSecond(MAX_SECOND, 999_999_999);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   241
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   242
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   243
     * Serialization version.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   244
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   245
    private static final long serialVersionUID = -665713676816604388L;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   246
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   247
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   248
     * The number of seconds from the epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   249
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   250
    private final long seconds;
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 nanoseconds, later along the time-line, from the seconds field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   253
     * This is always positive, and never exceeds 999,999,999.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   254
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   255
    private final int nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   256
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   257
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   258
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   259
     * Obtains the current instant from the system clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   260
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   261
     * This will query the {@link Clock#systemUTC() system UTC clock} to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   262
     * obtain the current instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   263
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   264
     * 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
   265
     * testing because the clock is effectively hard-coded.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   266
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   267
     * @return the current instant using the system clock, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   268
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   269
    public static Instant now() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   270
        return Clock.systemUTC().instant();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   271
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   272
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   273
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   274
     * Obtains the current instant from the specified clock.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   275
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   276
     * This will query the specified clock to obtain the current time.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   277
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   278
     * Using this method allows the use of an alternate clock for testing.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   279
     * The alternate clock may be introduced using {@link Clock dependency injection}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   280
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   281
     * @param clock  the clock to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   282
     * @return the current instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   283
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   284
    public static Instant now(Clock clock) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   285
        Objects.requireNonNull(clock, "clock");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   286
        return clock.instant();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   287
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   288
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   289
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   290
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   291
     * Obtains an instance of {@code Instant} using seconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   292
     * epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   293
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   294
     * The nanosecond field is set to zero.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   295
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   296
     * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   297
     * @return an instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   298
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   299
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   300
    public static Instant ofEpochSecond(long epochSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   301
        return create(epochSecond, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   302
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   303
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   304
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   305
     * Obtains an instance of {@code Instant} using seconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   306
     * epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   307
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   308
     * This method allows an arbitrary number of nanoseconds to be passed in.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   309
     * The factory will alter the values of the second and nanosecond in order
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   310
     * to ensure that the stored nanosecond is in the range 0 to 999,999,999.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   311
     * For example, the following will result in the exactly the same instant:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   312
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   313
     *  Instant.ofEpochSecond(3, 1);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   314
     *  Instant.ofEpochSecond(4, -999_999_999);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   315
     *  Instant.ofEpochSecond(2, 1000_000_001);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   316
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   317
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   318
     * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   319
     * @param nanoAdjustment  the nanosecond adjustment to the number of seconds, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   320
     * @return an instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   321
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   322
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   323
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   324
    public static Instant ofEpochSecond(long epochSecond, long nanoAdjustment) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   325
        long secs = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, NANOS_PER_SECOND));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   326
        int nos = (int)Math.floorMod(nanoAdjustment, NANOS_PER_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   327
        return create(secs, nos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   328
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   329
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   330
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   331
     * Obtains an instance of {@code Instant} using milliseconds from the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   332
     * epoch of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   333
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   334
     * The seconds and nanoseconds are extracted from the specified milliseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   335
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   336
     * @param epochMilli  the number of milliseconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   337
     * @return an instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   338
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   339
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   340
    public static Instant ofEpochMilli(long epochMilli) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   341
        long secs = Math.floorDiv(epochMilli, 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   342
        int mos = (int)Math.floorMod(epochMilli, 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   343
        return create(secs, mos * 1000_000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   344
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   345
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   346
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   347
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   348
     * Obtains an instance of {@code Instant} from a temporal object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   349
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   350
     * This obtains an instant based on the specified temporal.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   351
     * A {@code TemporalAccessor} represents an arbitrary set of date and time information,
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   352
     * which this factory converts to an instance of {@code Instant}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   353
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   354
     * The conversion extracts the {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   355
     * and {@link ChronoField#NANO_OF_SECOND NANO_OF_SECOND} fields.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   356
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   357
     * This method matches the signature of the functional interface {@link TemporalQuery}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   358
     * 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
   359
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   360
     * @param temporal  the temporal object to convert, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   361
     * @return the instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   362
     * @throws DateTimeException if unable to convert to an {@code Instant}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   363
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   364
    public static Instant from(TemporalAccessor temporal) {
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   365
        if (temporal instanceof Instant) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   366
            return (Instant) temporal;
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   367
        }
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
   368
        Objects.requireNonNull(temporal, "temporal");
20747
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   369
        try {
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   370
            long instantSecs = temporal.getLong(INSTANT_SECONDS);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   371
            int nanoOfSecond = temporal.get(NANO_OF_SECOND);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   372
            return Instant.ofEpochSecond(instantSecs, nanoOfSecond);
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   373
        } catch (DateTimeException ex) {
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   374
            throw new DateTimeException("Unable to obtain Instant from TemporalAccessor: " +
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   375
                    temporal + " of type " + temporal.getClass().getName());
86a86a94b367 8025718: Enhance error messages for parsing
rriggs
parents: 20521
diff changeset
   376
        }
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   377
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   378
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   379
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   380
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   381
     * 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
   382
     * {@code 2007-12-03T10:15:30.00Z}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   383
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   384
     * 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
   385
     * {@link DateTimeFormatter#ISO_INSTANT}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   386
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   387
     * @param text  the text to parse, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   388
     * @return the parsed instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   389
     * @throws DateTimeParseException if the text cannot be parsed
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   390
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   391
    public static Instant parse(final CharSequence text) {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   392
        return DateTimeFormatter.ISO_INSTANT.parse(text, Instant::from);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   393
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   394
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   395
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   396
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   397
     * Obtains an instance of {@code Instant} using seconds and nanoseconds.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   398
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   399
     * @param seconds  the length of the duration in seconds
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   400
     * @param nanoOfSecond  the nano-of-second, from 0 to 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   401
     * @throws DateTimeException if the instant exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   402
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   403
    private static Instant create(long seconds, int nanoOfSecond) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   404
        if ((seconds | nanoOfSecond) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   405
            return EPOCH;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   406
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   407
        if (seconds < MIN_SECOND || seconds > MAX_SECOND) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   408
            throw new DateTimeException("Instant exceeds minimum or maximum instant");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   409
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   410
        return new Instant(seconds, nanoOfSecond);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   411
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   412
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   413
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   414
     * Constructs an instance of {@code Instant} using seconds from the epoch of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   415
     * 1970-01-01T00:00:00Z and nanosecond fraction of second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   416
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   417
     * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   418
     * @param nanos  the nanoseconds within the second, must be positive
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   419
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   420
    private Instant(long epochSecond, int nanos) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   421
        super();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   422
        this.seconds = epochSecond;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   423
        this.nanos = nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   424
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   425
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   426
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   427
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   428
     * Checks if the specified field is supported.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   429
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   430
     * 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
   431
     * If false, then calling the {@link #range(TemporalField) range},
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   432
     * {@link #get(TemporalField) get} and {@link #with(TemporalField, long)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   433
     * methods will throw an exception.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   434
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   435
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   436
     * The supported fields are:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   437
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   438
     * <li>{@code NANO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   439
     * <li>{@code MICRO_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   440
     * <li>{@code MILLI_OF_SECOND}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   441
     * <li>{@code INSTANT_SECONDS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   442
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   443
     * All other {@code ChronoField} instances will return false.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   444
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   445
     * 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
   446
     * is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   447
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   448
     * Whether the field is supported is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   449
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   450
     * @param field  the field to check, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   451
     * @return true if the field is supported on this instant, false if not
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   452
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   453
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   454
    public boolean isSupported(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   455
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   456
            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
   457
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   458
        return field != null && field.isSupportedBy(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   459
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   460
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   461
    /**
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   462
     * Checks if the specified unit is supported.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   463
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   464
     * 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
   465
     * If false, then calling the {@link #plus(long, TemporalUnit)} and
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   466
     * {@link #minus(long, TemporalUnit) minus} methods will throw an exception.
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
     * 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
   469
     * The supported units are:
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   470
     * <ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   471
     * <li>{@code NANOS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   472
     * <li>{@code MICROS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   473
     * <li>{@code MILLIS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   474
     * <li>{@code SECONDS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   475
     * <li>{@code MINUTES}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   476
     * <li>{@code HOURS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   477
     * <li>{@code HALF_DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   478
     * <li>{@code DAYS}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   479
     * </ul>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   480
     * All other {@code ChronoUnit} instances will return false.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   481
     * <p>
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   482
     * 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
   483
     * is obtained by invoking {@code TemporalUnit.isSupportedBy(Temporal)}
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   484
     * passing {@code this} as the argument.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   485
     * Whether the unit is supported is determined by the unit.
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   486
     *
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   487
     * @param unit  the unit to check, null returns false
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   488
     * @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
   489
     */
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   490
    @Override
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   491
    public boolean isSupported(TemporalUnit unit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   492
        if (unit instanceof ChronoUnit) {
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   493
            return unit.isTimeBased() || unit == DAYS;
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   494
        }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   495
        return unit != null && unit.isSupportedBy(this);
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   496
    }
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   497
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
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   500
     * Gets the range of valid values for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   501
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   502
     * The range object expresses the minimum and maximum valid values for a field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   503
     * This instant is used to enhance the accuracy of the returned range.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   504
     * 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
   505
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   506
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   507
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   508
     * The {@link #isSupported(TemporalField) supported fields} will return
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   509
     * appropriate range instances.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   510
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   511
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   512
     * 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
   513
     * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   514
     * passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   515
     * Whether the range can be obtained is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   516
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   517
     * @param field  the field to query the range for, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   518
     * @return the range of valid values for the field, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   519
     * @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
   520
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   521
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   522
    @Override  // override for Javadoc
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   523
    public ValueRange range(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   524
        return Temporal.super.range(field);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   525
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   526
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   527
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   528
     * 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
   529
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   530
     * This queries this instant for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   531
     * 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
   532
     * 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
   533
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   534
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   535
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   536
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   537
     * values based on this date-time, except {@code INSTANT_SECONDS} which is too
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   538
     * 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
   539
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   540
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   541
     * 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
   542
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   543
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   544
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   545
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   546
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   547
     * @return the value for the field
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   548
     * @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
   549
     *         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
   550
     * @throws UnsupportedTemporalTypeException if the field is not supported or
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   551
     *         the range of values exceeds an {@code int}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   552
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   553
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   554
    @Override  // override for Javadoc and performance
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   555
    public int get(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   556
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   557
            switch ((ChronoField) field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   558
                case NANO_OF_SECOND: return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   559
                case MICRO_OF_SECOND: return nanos / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   560
                case MILLI_OF_SECOND: return nanos / 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   561
                case INSTANT_SECONDS: INSTANT_SECONDS.checkValidIntValue(seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   562
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   563
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   564
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   565
        return range(field).checkValidIntValue(field.getFrom(this), field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   566
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   567
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   568
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   569
     * 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
   570
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   571
     * This queries this instant for the value for the specified field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   572
     * 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
   573
     * or for some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   574
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   575
     * If the field is a {@link ChronoField} then the query is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   576
     * The {@link #isSupported(TemporalField) supported fields} will return valid
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   577
     * values based on this date-time.
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   578
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   579
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   580
     * 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
   581
     * is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   582
     * passing {@code this} as the argument. Whether the value can be obtained,
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   583
     * and what the value represents, is determined by the field.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   584
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   585
     * @param field  the field to get, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   586
     * @return the value for the field
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   587
     * @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
   588
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   589
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   590
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   591
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   592
    public long getLong(TemporalField field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   593
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   594
            switch ((ChronoField) field) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   595
                case NANO_OF_SECOND: return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   596
                case MICRO_OF_SECOND: return nanos / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   597
                case MILLI_OF_SECOND: return nanos / 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   598
                case INSTANT_SECONDS: return seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   599
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   600
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   601
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   602
        return field.getFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   603
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   604
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   605
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   606
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   607
     * 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
   608
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   609
     * The epoch second count is a simple incrementing count of seconds where
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   610
     * second 0 is 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   611
     * The nanosecond part of the day is returned by {@code getNanosOfSecond}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   612
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   613
     * @return the seconds from the epoch of 1970-01-01T00:00:00Z
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   614
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   615
    public long getEpochSecond() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   616
        return seconds;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   617
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   618
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   619
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   620
     * Gets the number of nanoseconds, later along the time-line, from the start
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   621
     * of the second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   622
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   623
     * The nanosecond-of-second value measures the total number of nanoseconds from
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   624
     * the second returned by {@code getEpochSecond}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   625
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   626
     * @return the nanoseconds within the second, always positive, never exceeds 999,999,999
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   627
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   628
    public int getNano() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   629
        return nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   630
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   631
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   632
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   633
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   634
     * Returns an adjusted copy of this instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   635
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   636
     * 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
   637
     * The adjustment takes place using the specified adjuster strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   638
     * Read the documentation of the adjuster to understand what adjustment will be made.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   639
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   640
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   641
     * {@link TemporalAdjuster#adjustInto(Temporal)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   642
     * specified adjuster passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   643
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   644
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   645
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   646
     * @param adjuster the adjuster to use, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   647
     * @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
   648
     * @throws DateTimeException if the adjustment cannot be made
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   649
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   650
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   651
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   652
    public Instant with(TemporalAdjuster adjuster) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   653
        return (Instant) adjuster.adjustInto(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   654
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   655
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   656
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   657
     * 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
   658
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   659
     * This returns an {@code Instant}, based on this one, with the value
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   660
     * for the specified field changed.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   661
     * 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
   662
     * some other reason, an exception is thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   663
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   664
     * If the field is a {@link ChronoField} then the adjustment is implemented here.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   665
     * The supported fields behave as follows:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   666
     * <ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   667
     * <li>{@code NANO_OF_SECOND} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   668
     *  Returns an {@code Instant} with the specified nano-of-second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   669
     *  The epoch-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   670
     * <li>{@code MICRO_OF_SECOND} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   671
     *  Returns an {@code Instant} with the nano-of-second replaced by the specified
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   672
     *  micro-of-second multiplied by 1,000. The epoch-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   673
     * <li>{@code MILLI_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
     *  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
   676
     * <li>{@code INSTANT_SECONDS} -
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   677
     *  Returns an {@code Instant} with the specified epoch-second.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   678
     *  The nano-of-second will be unchanged.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   679
     * </ul>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   680
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   681
     * 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
   682
     * then a {@code DateTimeException} will be thrown.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   683
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   684
     * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   685
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   686
     * 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
   687
     * is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   688
     * passing {@code this} as the argument. In this case, the field determines
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   689
     * whether and how to adjust the instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   690
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   691
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   692
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   693
     * @param field  the field to set in the result, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   694
     * @param newValue  the new value of the field in the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   695
     * @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
   696
     * @throws DateTimeException if the field cannot be set
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   697
     * @throws UnsupportedTemporalTypeException if the field is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   698
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   699
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   700
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   701
    public Instant with(TemporalField field, long newValue) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   702
        if (field instanceof ChronoField) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   703
            ChronoField f = (ChronoField) field;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   704
            f.checkValidValue(newValue);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   705
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   706
                case MILLI_OF_SECOND: {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   707
                    int nval = (int) newValue * 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   708
                    return (nval != nanos ? create(seconds, nval) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   709
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   710
                case MICRO_OF_SECOND: {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   711
                    int nval = (int) newValue * 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   712
                    return (nval != nanos ? create(seconds, nval) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   713
                }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   714
                case NANO_OF_SECOND: return (newValue != nanos ? create(seconds, (int) newValue) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   715
                case INSTANT_SECONDS: return (newValue != seconds ? create(newValue, nanos) : this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   716
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   717
            throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   718
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   719
        return field.adjustInto(this, newValue);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   720
    }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   721
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   722
    //-----------------------------------------------------------------------
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
     * Returns a copy of this {@code Instant} truncated to the specified unit.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   725
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   726
     * Truncating the instant returns a copy of the original with fields
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   727
     * smaller than the specified unit set to zero.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   728
     * 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
   729
     * in {@code toString}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   730
     * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   731
     * 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
   732
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   733
     * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   734
     * that divides into the length of a standard day without remainder.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   735
     * This includes all supplied time units on {@link ChronoUnit} and
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   736
     * {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   737
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   738
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   739
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   740
     * @param unit  the unit to truncate to, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   741
     * @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
   742
     * @throws DateTimeException if the unit is invalid for truncation
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   743
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   744
     */
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   745
    public Instant truncatedTo(TemporalUnit unit) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   746
        if (unit == ChronoUnit.NANOS) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   747
            return this;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   748
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   749
        Duration unitDur = unit.getDuration();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   750
        if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   751
            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
   752
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   753
        long dur = unitDur.toNanos();
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   754
        if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   755
            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
   756
        }
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   757
        long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   758
        long result = (nod / dur) * dur;
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   759
        return plusNanos(result - nod);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   760
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   761
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   762
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   763
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   764
     * Returns a copy of this instant with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   765
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   766
     * 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
   767
     * 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
   768
     * the {@link TemporalAmount} interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   769
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   770
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   771
     * {@link TemporalAmount#addTo(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   772
     * to implement the addition in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   773
     * calls back to {@link #plus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   774
     * of the amount implementation to determine if it can be successfully added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   775
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   776
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   777
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   778
     * @param amountToAdd  the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   779
     * @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
   780
     * @throws DateTimeException if the addition cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   781
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   782
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   783
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   784
    public Instant plus(TemporalAmount amountToAdd) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   785
        return (Instant) amountToAdd.addTo(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   786
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   787
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   788
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   789
     * Returns a copy of this instant with the specified amount added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   790
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   791
     * This returns an {@code Instant}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   792
     * 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
   793
     * 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
   794
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   795
     * 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
   796
     * The supported fields behave as follows:
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   797
     * <ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   798
     * <li>{@code NANOS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   799
     *  Returns a {@code Instant} with the specified number of nanoseconds added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   800
     *  This is equivalent to {@link #plusNanos(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   801
     * <li>{@code MICROS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   802
     *  Returns a {@code Instant} with the specified number of microseconds added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   803
     *  This is equivalent to {@link #plusNanos(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   804
     *  multiplied by 1,000.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   805
     * <li>{@code MILLIS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   806
     *  Returns a {@code Instant} with the specified number of milliseconds added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   807
     *  This is equivalent to {@link #plusNanos(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   808
     *  multiplied by 1,000,000.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   809
     * <li>{@code SECONDS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   810
     *  Returns a {@code Instant} with the specified number of seconds added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   811
     *  This is equivalent to {@link #plusSeconds(long)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   812
     * <li>{@code MINUTES} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   813
     *  Returns a {@code Instant} with the specified number of minutes added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   814
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   815
     *  multiplied by 60.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   816
     * <li>{@code HOURS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   817
     *  Returns a {@code Instant} with the specified number of hours added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   818
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   819
     *  multiplied by 3,600.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   820
     * <li>{@code HALF_DAYS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   821
     *  Returns a {@code Instant} with the specified number of half-days added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   822
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   823
     *  multiplied by 43,200 (12 hours).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   824
     * <li>{@code DAYS} -
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   825
     *  Returns a {@code Instant} with the specified number of days added.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   826
     *  This is equivalent to {@link #plusSeconds(long)} with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   827
     *  multiplied by 86,400 (24 hours).
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   828
     * </ul>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   829
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   830
     * All other {@code ChronoUnit} instances will throw an {@code UnsupportedTemporalTypeException}.
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   831
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   832
     * 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
   833
     * is obtained by invoking {@code TemporalUnit.addTo(Temporal, long)}
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   834
     * passing {@code this} as the argument. In this case, the unit determines
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   835
     * whether and how to perform the addition.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   836
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   837
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   838
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   839
     * @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
   840
     * @param unit  the unit of the amount to add, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   841
     * @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
   842
     * @throws DateTimeException if the addition cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   843
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   844
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   845
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   846
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   847
    public Instant plus(long amountToAdd, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   848
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   849
            switch ((ChronoUnit) unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   850
                case NANOS: return plusNanos(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   851
                case MICROS: return plus(amountToAdd / 1000_000, (amountToAdd % 1000_000) * 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   852
                case MILLIS: return plusMillis(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   853
                case SECONDS: return plusSeconds(amountToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   854
                case MINUTES: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_MINUTE));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   855
                case HOURS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_HOUR));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   856
                case HALF_DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY / 2));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   857
                case DAYS: return plusSeconds(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY));
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   858
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
   859
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   860
        }
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   861
        return unit.addTo(this, amountToAdd);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   862
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   863
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   864
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   865
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   866
     * Returns a copy of this instant with the specified duration in seconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   867
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   868
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   869
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   870
     * @param secondsToAdd  the seconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   871
     * @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
   872
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   873
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   874
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   875
    public Instant plusSeconds(long secondsToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   876
        return plus(secondsToAdd, 0);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   877
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   878
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   879
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   880
     * Returns a copy of this instant with the specified duration in milliseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   881
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   882
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   883
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   884
     * @param millisToAdd  the milliseconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   885
     * @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
   886
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   887
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   888
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   889
    public Instant plusMillis(long millisToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   890
        return plus(millisToAdd / 1000, (millisToAdd % 1000) * 1000_000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   891
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   892
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   893
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   894
     * Returns a copy of this instant with the specified duration in nanoseconds added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   895
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   896
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   897
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   898
     * @param nanosToAdd  the nanoseconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   899
     * @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
   900
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   901
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   902
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   903
    public Instant plusNanos(long nanosToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   904
        return plus(0, nanosToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   905
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   906
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   907
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   908
     * Returns a copy of this instant with the specified duration added.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   909
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   910
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   911
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   912
     * @param secondsToAdd  the seconds to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   913
     * @param nanosToAdd  the nanos to add, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   914
     * @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
   915
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   916
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   917
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   918
    private Instant plus(long secondsToAdd, long nanosToAdd) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   919
        if ((secondsToAdd | nanosToAdd) == 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   920
            return this;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   921
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   922
        long epochSec = Math.addExact(seconds, secondsToAdd);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   923
        epochSec = Math.addExact(epochSec, nanosToAdd / NANOS_PER_SECOND);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   924
        nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   925
        long nanoAdjustment = nanos + nanosToAdd;  // safe int+NANOS_PER_SECOND
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   926
        return ofEpochSecond(epochSec, nanoAdjustment);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   927
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   928
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   929
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   930
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   931
     * Returns a copy of this instant with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   932
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   933
     * 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
   934
     * 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
   935
     * the {@link TemporalAmount} interface.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   936
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   937
     * The calculation is delegated to the amount object by calling
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   938
     * {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   939
     * to implement the subtraction in any way it wishes, however it typically
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   940
     * calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   941
     * of the amount implementation to determine if it can be successfully subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   942
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   943
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   944
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   945
     * @param amountToSubtract  the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   946
     * @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
   947
     * @throws DateTimeException if the subtraction cannot be made
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   948
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   949
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   950
    @Override
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   951
    public Instant minus(TemporalAmount amountToSubtract) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   952
        return (Instant) amountToSubtract.subtractFrom(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   953
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   954
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   955
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   956
     * Returns a copy of this instant with the specified amount subtracted.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   957
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   958
     * This returns a {@code Instant}, based on this one, with the amount
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   959
     * 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
   960
     * 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
   961
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   962
     * 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
   963
     * 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
   964
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   965
     * This instance is immutable and unaffected by this method call.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   966
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   967
     * @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
   968
     * @param unit  the unit of the amount to subtract, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   969
     * @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
   970
     * @throws DateTimeException if the subtraction cannot be made
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
   971
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
   972
     * @throws ArithmeticException if numeric overflow occurs
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   973
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   974
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   975
    public Instant minus(long amountToSubtract, TemporalUnit unit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   976
        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
   977
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   978
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   979
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   980
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   981
     * Returns a copy of this instant with the specified duration in seconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   982
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   983
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   984
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   985
     * @param secondsToSubtract  the seconds to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   986
     * @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
   987
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   988
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   989
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   990
    public Instant minusSeconds(long secondsToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   991
        if (secondsToSubtract == Long.MIN_VALUE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   992
            return plusSeconds(Long.MAX_VALUE).plusSeconds(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   993
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   994
        return plusSeconds(-secondsToSubtract);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   995
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   996
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   997
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   998
     * Returns a copy of this instant with the specified duration in milliseconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
   999
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1000
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1001
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1002
     * @param millisToSubtract  the milliseconds to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1003
     * @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
  1004
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1005
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1006
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1007
    public Instant minusMillis(long millisToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1008
        if (millisToSubtract == Long.MIN_VALUE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1009
            return plusMillis(Long.MAX_VALUE).plusMillis(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1010
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1011
        return plusMillis(-millisToSubtract);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1012
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1013
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1014
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1015
     * Returns a copy of this instant with the specified duration in nanoseconds subtracted.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1016
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1017
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1018
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1019
     * @param nanosToSubtract  the nanoseconds to subtract, positive or negative
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1020
     * @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
  1021
     * @throws DateTimeException if the result exceeds the maximum or minimum instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1022
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1023
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1024
    public Instant minusNanos(long nanosToSubtract) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1025
        if (nanosToSubtract == Long.MIN_VALUE) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1026
            return plusNanos(Long.MAX_VALUE).plusNanos(1);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1027
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1028
        return plusNanos(-nanosToSubtract);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1029
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1030
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1031
    //-------------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1032
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1033
     * Queries this instant using the specified query.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1034
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1035
     * This queries this instant using the specified query strategy object.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1036
     * The {@code TemporalQuery} object defines the logic to be used to
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1037
     * obtain the result. Read the documentation of the query to understand
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1038
     * what the result of this method will be.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1039
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1040
     * The result of this method is obtained by invoking the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1041
     * {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1042
     * specified query passing {@code this} as the argument.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1043
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1044
     * @param <R> the type of the result
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1045
     * @param query  the query to invoke, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1046
     * @return the query result, null may be returned (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1047
     * @throws DateTimeException if unable to query (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1048
     * @throws ArithmeticException if numeric overflow occurs (defined by the query)
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1049
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1050
    @SuppressWarnings("unchecked")
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1051
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1052
    public <R> R query(TemporalQuery<R> query) {
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1053
        if (query == TemporalQueries.precision()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1054
            return (R) NANOS;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1055
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1056
        // inline TemporalAccessor.super.query(query) as an optimization
20795
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1057
        if (query == TemporalQueries.chronology() || query == TemporalQueries.zoneId() ||
8ec9e5b79828 8025722: TemporalAdjusters and TemporalQueries
rriggs
parents: 20747
diff changeset
  1058
                query == TemporalQueries.zone() || query == TemporalQueries.offset()) {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1059
            return null;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1060
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1061
        return query.queryFrom(this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1062
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1063
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1064
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1065
     * Adjusts the specified temporal object to have this instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1066
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1067
     * This returns a temporal object of the same observable type as the input
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1068
     * with the instant changed to be the same as this.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1069
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1070
     * The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1071
     * twice, passing {@link ChronoField#INSTANT_SECONDS} and
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1072
     * {@link ChronoField#NANO_OF_SECOND} as the fields.
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
     * In most cases, it is clearer to reverse the calling pattern by using
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1075
     * {@link Temporal#with(TemporalAdjuster)}:
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1076
     * <pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1077
     *   // these two lines are equivalent, but the second approach is recommended
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1078
     *   temporal = thisInstant.adjustInto(temporal);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1079
     *   temporal = temporal.with(thisInstant);
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
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1082
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1083
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1084
     * @param temporal  the target object to be adjusted, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1085
     * @return the adjusted object, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1086
     * @throws DateTimeException if unable to make the adjustment
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1087
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1088
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1089
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1090
    public Temporal adjustInto(Temporal temporal) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1091
        return temporal.with(INSTANT_SECONDS, seconds).with(NANO_OF_SECOND, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1092
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1093
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1094
    /**
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1095
     * 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
  1096
     * <p>
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1097
     * This calculates the amount of time between two {@code Instant}
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1098
     * objects in terms of a single {@code TemporalUnit}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1099
     * The start and end points are {@code this} and the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1100
     * The result will be negative if the end is before the start.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1101
     * The calculation returns a whole number, representing the number of
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1102
     * complete units between the two instants.
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1103
     * 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
  1104
     * {@code Instant} using {@link #from(TemporalAccessor)}.
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1105
     * For example, the amount in days between two dates can be calculated
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1106
     * using {@code startInstant.until(endInstant, SECONDS)}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1107
     * <p>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1108
     * There are two equivalent ways of using this method.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1109
     * The first is to invoke this method.
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1110
     * The second is to use {@link TemporalUnit#between(Temporal, Temporal)}:
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1111
     * <pre>
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1112
     *   // these two lines are equivalent
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1113
     *   amount = start.until(end, SECONDS);
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1114
     *   amount = SECONDS.between(start, end);
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
     * 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
  1117
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1118
     * The calculation is implemented in this method for {@link ChronoUnit}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1119
     * The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1120
     * {@code MINUTES}, {@code HOURS}, {@code HALF_DAYS} and {@code DAYS}
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1121
     * are supported. Other {@code ChronoUnit} values will throw an exception.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1122
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1123
     * 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
  1124
     * is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1125
     * 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
  1126
     * as the second argument.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1127
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1128
     * This instance is immutable and unaffected by this method call.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1129
     *
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1130
     * @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
  1131
     * @param unit  the unit to measure the amount in, not null
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 16852
diff changeset
  1132
     * @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
  1133
     * @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
  1134
     *  temporal cannot be converted to an {@code Instant}
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1135
     * @throws UnsupportedTemporalTypeException if the unit is not supported
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1136
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1137
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1138
    @Override
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1139
    public long until(Temporal endExclusive, TemporalUnit unit) {
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1140
        Instant end = Instant.from(endExclusive);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1141
        if (unit instanceof ChronoUnit) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1142
            ChronoUnit f = (ChronoUnit) unit;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1143
            switch (f) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1144
                case NANOS: return nanosUntil(end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1145
                case MICROS: return nanosUntil(end) / 1000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1146
                case MILLIS: return Math.subtractExact(end.toEpochMilli(), toEpochMilli());
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1147
                case SECONDS: return secondsUntil(end);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1148
                case MINUTES: return secondsUntil(end) / SECONDS_PER_MINUTE;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1149
                case HOURS: return secondsUntil(end) / SECONDS_PER_HOUR;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1150
                case HALF_DAYS: return secondsUntil(end) / (12 * SECONDS_PER_HOUR);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1151
                case DAYS: return secondsUntil(end) / (SECONDS_PER_DAY);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1152
            }
19030
32f129cb6351 8016025: JSR 310 DateTime API Updates IV
sherman
parents: 17474
diff changeset
  1153
            throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1154
        }
20520
0952771e3e25 8024835: Change until() to accept any compatible temporal
rriggs
parents: 19841
diff changeset
  1155
        return unit.between(this, end);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1156
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1157
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1158
    private long nanosUntil(Instant end) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1159
        long secsDiff = Math.subtractExact(end.seconds, seconds);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1160
        long totalNanos = Math.multiplyExact(secsDiff, NANOS_PER_SECOND);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1161
        return Math.addExact(totalNanos, end.nanos - nanos);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1162
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1163
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1164
    private long secondsUntil(Instant end) {
16852
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1165
        long secsDiff = Math.subtractExact(end.seconds, seconds);
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1166
        long nanosDiff = end.nanos - nanos;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1167
        if (secsDiff > 0 && nanosDiff < 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1168
            secsDiff--;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1169
        } else if (secsDiff < 0 && nanosDiff > 0) {
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1170
            secsDiff++;
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1171
        }
60207b2b4b42 8011172: JSR 310 DateTime API Updates II
sherman
parents: 15658
diff changeset
  1172
        return secsDiff;
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1173
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1174
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1175
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1176
    /**
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1177
     * Combines this instant with an offset to create an {@code OffsetDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1178
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1179
     * This returns an {@code OffsetDateTime} formed from this instant at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1180
     * specified offset from UTC/Greenwich. An exception will be thrown if the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1181
     * instant is too large to fit into an offset date-time.
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 method is equivalent to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1184
     * {@link OffsetDateTime#ofInstant(Instant, ZoneId) OffsetDateTime.ofInstant(this, offset)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1185
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1186
     * @param offset  the offset to combine with, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1187
     * @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
  1188
     * @throws DateTimeException if the result exceeds the supported range
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
    public OffsetDateTime atOffset(ZoneOffset offset) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1191
        return OffsetDateTime.ofInstant(this, offset);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1192
    }
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
    /**
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1195
     * Combines this instant with a time-zone to create a {@code ZonedDateTime}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1196
     * <p>
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1197
     * This returns an {@code ZonedDateTime} formed from this instant at the
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1198
     * 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
  1199
     * large to fit into a zoned date-time.
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 method is equivalent to
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1202
     * {@link ZonedDateTime#ofInstant(Instant, ZoneId) ZonedDateTime.ofInstant(this, zone)}.
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1203
     *
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1204
     * @param zone  the zone to combine with, not null
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1205
     * @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
  1206
     * @throws DateTimeException if the result exceeds the supported range
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
    public ZonedDateTime atZone(ZoneId zone) {
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1209
        return ZonedDateTime.ofInstant(this, zone);
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1210
    }
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
    //-----------------------------------------------------------------------
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1213
    /**
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1214
     * Converts this instant to the number of milliseconds from the epoch
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1215
     * of 1970-01-01T00:00:00Z.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1216
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1217
     * 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
  1218
     * 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
  1219
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1220
     * If this instant has greater than millisecond precision, then the conversion
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1221
     * will drop any excess precision information as though the amount in nanoseconds
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1222
     * was subject to integer division by one million.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1223
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1224
     * @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
  1225
     * @throws ArithmeticException if numeric overflow occurs
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1226
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1227
    public long toEpochMilli() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1228
        long millis = Math.multiplyExact(seconds, 1000);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1229
        return millis + nanos / 1000_000;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1230
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1231
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1232
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1233
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1234
     * Compares this instant to the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1235
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1236
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1237
     * It is "consistent with equals", as defined by {@link Comparable}.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1238
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1239
     * @param otherInstant  the other instant to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1240
     * @return the comparator value, negative if less, positive if greater
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1241
     * @throws NullPointerException if otherInstant is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1242
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1243
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1244
    public int compareTo(Instant otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1245
        int cmp = Long.compare(seconds, otherInstant.seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1246
        if (cmp != 0) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1247
            return cmp;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1248
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1249
        return nanos - otherInstant.nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1250
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1251
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1252
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1253
     * Checks if this instant is after the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1254
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1255
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1256
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1257
     * @param otherInstant  the other instant to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1258
     * @return true if this instant is after the specified instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1259
     * @throws NullPointerException if otherInstant is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1260
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1261
    public boolean isAfter(Instant otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1262
        return compareTo(otherInstant) > 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1263
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1264
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1265
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1266
     * Checks if this instant is before the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1267
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1268
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1269
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1270
     * @param otherInstant  the other instant to compare to, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1271
     * @return true if this instant is before the specified instant
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1272
     * @throws NullPointerException if otherInstant is null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1273
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1274
    public boolean isBefore(Instant otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1275
        return compareTo(otherInstant) < 0;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1276
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1277
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1278
    //-----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1279
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1280
     * Checks if this instant is equal to the specified instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1281
     * <p>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1282
     * The comparison is based on the time-line position of the instants.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1283
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1284
     * @param otherInstant  the other instant, null returns false
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1285
     * @return true if the other instant is equal to this one
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1286
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1287
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1288
    public boolean equals(Object otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1289
        if (this == otherInstant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1290
            return true;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1291
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1292
        if (otherInstant instanceof Instant) {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1293
            Instant other = (Instant) otherInstant;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1294
            return this.seconds == other.seconds &&
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1295
                   this.nanos == other.nanos;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1296
        }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1297
        return false;
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1298
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1299
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1300
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1301
     * Returns a hash code for this instant.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1302
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1303
     * @return a suitable hash code
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1304
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1305
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1306
    public int hashCode() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1307
        return ((int) (seconds ^ (seconds >>> 32))) + 51 * nanos;
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
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1312
     * A string representation of this instant using ISO-8601 representation.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1313
     * <p>
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1314
     * The format used is the same as {@link DateTimeFormatter#ISO_INSTANT}.
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1315
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1316
     * @return an ISO-8601 representation of this instant, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1317
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1318
    @Override
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1319
    public String toString() {
15658
55b829ca2334 8007392: JSR 310: DateTime API Updates
sherman
parents: 15289
diff changeset
  1320
        return DateTimeFormatter.ISO_INSTANT.format(this);
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1321
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1322
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1323
    // -----------------------------------------------------------------------
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1324
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1325
     * Writes the object using a
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1326
     * <a href="../../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
  1327
     * @serialData
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1328
     * <pre>
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1329
     *  out.writeByte(2);  // identifies an Instant
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1330
     *  out.writeLong(seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1331
     *  out.writeInt(nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1332
     * </pre>
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1333
     *
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1334
     * @return the instance of {@code Ser}, not null
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1335
     */
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1336
    private Object writeReplace() {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1337
        return new Ser(Ser.INSTANT_TYPE, this);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1338
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1339
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1340
    /**
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1341
     * Defend against malicious streams.
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1342
     * @return never
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1343
     * @throws InvalidObjectException always
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1344
     */
19841
15c8e97d6a14 8024164: JSR310 serialization should be described in details
rriggs
parents: 19030
diff changeset
  1345
    private Object readResolve() throws InvalidObjectException {
15289
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1346
        throw new InvalidObjectException("Deserialization via serialization delegate");
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1347
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1348
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1349
    void writeExternal(DataOutput out) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1350
        out.writeLong(seconds);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1351
        out.writeInt(nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1352
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1353
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1354
    static Instant readExternal(DataInput in) throws IOException {
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1355
        long seconds = in.readLong();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1356
        int nanos = in.readInt();
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1357
        return Instant.ofEpochSecond(seconds, nanos);
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1358
    }
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1359
3ac550392e43 8003680: JSR 310 Date/Time API
sherman
parents:
diff changeset
  1360
}