This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.
The range of an instant requires the storage of a number larger than a long
.
To achieve this, the class stores a long
representing epoch-seconds and an
int
representing nanosecond-of-second, which will always be between 0 and 999,999,999.
The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z
where instants after the epoch have positive values, and earlier instants have negative values.
For both the epoch-second and nanosecond parts, a larger value is always later on the time-line
than a smaller value.
The length of the solar day is the standard way that humans measure time. This has traditionally been subdivided into 24 hours of 60 minutes of 60 seconds, forming a 86400 second day.
Modern timekeeping is based on atomic clocks which precisely define an SI second relative to the transitions of a Caesium atom. The length of an SI second was defined to be very close to the 86400th fraction of a day.
Unfortunately, as the Earth rotates the length of the day varies. In addition, over time the average length of the day is getting longer as the Earth slows. As a result, the length of a solar day in 2012 is slightly longer than 86400 SI seconds. The actual length of any given day and the amount by which the Earth is slowing are not predictable and can only be determined by measurement. The UT1 time-scale captures the accurate length of day, but is only available some time after the day has completed.
The UTC time-scale is a standard approach to bundle up all the additional fractions of a second from UT1 into whole seconds, known as leap-seconds. A leap-second may be added or removed depending on the Earth's rotational changes. As such, UTC permits a day to have 86399 SI seconds or 86401 SI seconds where necessary in order to keep the day aligned with the Sun.
The modern UTC time-scale was introduced in 1972, introducing the concept of whole leap-seconds. Between 1958 and 1972, the definition of UTC was complex, with minor sub-second leaps and alterations to the length of the notional second. As of 2012, discussions are underway to change the definition of UTC again, with the potential to remove leap seconds or introduce other changes.
Given the complexity of accurate timekeeping described above, this Java API defines its own time-scale, the Java Time-Scale.
The Java Time-Scale divides each calendar day into exactly 86400 subdivisions, known as seconds. These seconds may differ from the SI second. It closely matches the de facto international civil time scale, the definition of which changes from time to time.
The Java Time-Scale has slightly different definitions for different segments of the time-line, each based on the consensus international time scale that is used as the basis for civil time. Whenever the internationally-agreed time scale is modified or replaced, a new segment of the Java Time-Scale must be defined for it. Each segment must meet these requirements:
For the segment from 1972-11-03 (exact boundary discussed below) until further notice, the consensus international time scale is UTC (with leap seconds). In this segment, the Java Time-Scale is identical to UTC-SLS. This is identical to UTC on days that do not have a leap second. On days that do have a leap second, the leap second is spread equally over the last 1000 seconds of the day, maintaining the appearance of exactly 86400 seconds per day.
For the segment prior to 1972-11-03, extending back arbitrarily far, the consensus international time scale is defined to be UT1, applied proleptically, which is equivalent to the (mean) solar time on the prime meridian (Greenwich). In this segment, the Java Time-Scale is identical to the consensus international time scale. The exact boundary between the two segments is the instant where UT1 = UTC between 1972-11-03T00:00 and 1972-11-04T12:00.
Implementations of the Java time-scale using the JSR-310 API are not
required to provide any clock that is sub-second accurate, or that
progresses monotonically or smoothly. Implementations are therefore
not required to actually perform the UTC-SLS slew or to otherwise be
aware of leap seconds. JSR-310 does, however, require that
implementations must document the approach they use when defining a
clock representing the current instant.
See Clock
for details on the available clocks.
The Java time-scale is used for all date-time classes.
This includes Instant
, LocalDate
, LocalTime
, OffsetDateTime
,
ZonedDateTime
and Duration
.
This is a value-based
class; programmers should treat instances that are
equal as interchangeable and should not
use instances for synchronization, or unpredictable behavior may
occur. For example, in a future release, synchronization may fail.
The equals
method should be used for comparisons.
Implementation Specification
This class is immutable and thread-safe.
Modifier and Type | Field and Description |
---|---|
public static final Instant | EPOCH
Constant for the 1970-01-01T00:00:00Z epoch instant. |
public static final Instant | MAX
The maximum supported |
private static final long | MAX_SECOND
The maximum supported epoch second. |
public static final Instant | MIN
The minimum supported |
private static final long | MIN_SECOND
The minimum supported epoch second. |
private final int | nanos
The number of nanoseconds, later along the time-line, from the seconds field. |
private final long | seconds
The number of seconds from the epoch of 1970-01-01T00:00:00Z. |
private static final long | serialVersionUID
Serialization version. |
Access | Constructor and Description |
---|---|
private | Instant(long
the number of seconds from 1970-01-01T00:00:00Z epochSecond, int the nanoseconds within the second, must be positive nanos)Constructs an instance of |
Modifier and Type | Method and Description |
---|---|
public Temporal | Returns: the adjusted object, not nullthe target object to be adjusted, not null temporal)Implements java. Adjusts the specified temporal object to have this instant. |
public OffsetDateTime | Returns: the offset date-time formed from this instant and the specified offset, not nullthe offset to combine with, not null offset)Combines this instant with an offset to create an |
public ZonedDateTime | |
public int | Returns: the comparator value, that is less than zero if this instant is beforeotherInstant ,
zero if they are equal, or greater than zero if this instant is after otherInstant the other instant to compare to, not null otherInstant)Implements java. Compares this instant to the specified instant. |
private static Instant | create(long
the length of the duration in seconds seconds, int the nano-of-second, from 0 to 999,999,999 nanoOfSecond)Obtains an instance of |
public boolean | Returns: true if the other instant is equal to this onethe other instant, null returns false other)Overrides java. Checks if this instant is equal to the specified instant. |
public static Instant | Returns: the instant, not nullthe temporal object to convert, not null temporal)Obtains an instance of |
public int | Returns: the value for the fieldthe field to get, not null field)Overrides default java. Gets the value of the specified field from this instant as an |
public long | Returns: the seconds from the epoch of 1970-01-01T00:00:00ZGets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z. |
public long | Returns: the value for the fieldthe field to get, not null field)Implements java. Gets the value of the specified field from this instant as a |
public int | Returns: the nanoseconds within the second, always positive, never exceeds 999,999,999Gets the number of nanoseconds, later along the time-line, from the start of the second. |
public int | Returns: a suitable hash codeOverrides java. Returns a hash code for this instant. |
public boolean | |
public boolean | |
public boolean | Returns: true if the field is supported on this instant, false if notthe field to check, null returns false field)Implements java. Checks if the specified field is supported. |
public boolean | Returns: true if the unit can be added/subtracted, false if notthe unit to check, null returns false unit)Implements java. Checks if the specified unit is supported. |
private long | |
private long | |
public Instant | Returns: anInstant based on this instant with the subtraction made, not nullthe amount to subtract, not null amountToSubtract)Overrides default java. Returns a copy of this instant with the specified amount subtracted. |
public Instant | Returns: anInstant based on this instant with the specified amount subtracted, not nullthe amount of the unit to subtract from the result, may be negative amountToSubtract, TemporalUnit the unit of the amount to subtract, not null unit)Overrides default java. Returns a copy of this instant with the specified amount subtracted. |
public Instant | Returns: anInstant based on this instant with the specified milliseconds subtracted, not nullthe milliseconds to subtract, positive or negative millisToSubtract)Returns a copy of this instant with the specified duration in milliseconds subtracted. |
public Instant | Returns: anInstant based on this instant with the specified nanoseconds subtracted, not nullthe nanoseconds to subtract, positive or negative nanosToSubtract)Returns a copy of this instant with the specified duration in nanoseconds subtracted. |
public Instant | Returns: anInstant based on this instant with the specified seconds subtracted, not nullthe seconds to subtract, positive or negative secondsToSubtract)Returns a copy of this instant with the specified duration in seconds subtracted. |
private long | |
public static Instant | Returns: the current instant using the system clock, not nullObtains the current instant from the system clock. |
public static Instant | |
public static Instant | Returns: an instant, not nullthe number of milliseconds from 1970-01-01T00:00:00Z epochMilli)Obtains an instance of |
public static Instant | Returns: an instant, not nullthe number of seconds from 1970-01-01T00:00:00Z epochSecond)Obtains an instance of |
public static Instant | Returns: an instant, not nullthe number of seconds from 1970-01-01T00:00:00Z epochSecond, long the nanosecond adjustment to the number of seconds, positive or negative nanoAdjustment)Obtains an instance of |
public static Instant | Returns: the parsed instant, not nullthe text to parse, not null text)Obtains an instance of |
public Instant | Returns: anInstant based on this instant with the addition made, not nullthe amount to add, not null amountToAdd)Overrides default java. Returns a copy of this instant with the specified amount added. |
public Instant | Returns: anInstant based on this instant with the specified amount added, not nullthe amount of the unit to add to the result, may be negative amountToAdd, TemporalUnit the unit of the amount to add, not null unit)Implements java. Returns a copy of this instant with the specified amount added. |
private Instant | Returns: anInstant based on this instant with the specified seconds added, not nullthe seconds to add, positive or negative secondsToAdd, long the nanos to add, positive or negative nanosToAdd)Returns a copy of this instant with the specified duration added. |
public Instant | Returns: anInstant based on this instant with the specified milliseconds added, not nullthe milliseconds to add, positive or negative millisToAdd)Returns a copy of this instant with the specified duration in milliseconds added. |
public Instant | Returns: anInstant based on this instant with the specified nanoseconds added, not nullthe nanoseconds to add, positive or negative nanosToAdd)Returns a copy of this instant with the specified duration in nanoseconds added. |
public Instant | Returns: anInstant based on this instant with the specified seconds added, not nullthe seconds to add, positive or negative secondsToAdd)Returns a copy of this instant with the specified duration in seconds added. |
public < the type of the result R> R | Returns: the query result, null may be returned (defined by the query)the query to invoke, not null query)Overrides default java. Queries this instant using the specified query. |
public ValueRange | Returns: the range of valid values for the field, not nullthe field to query the range for, not null field)Overrides default java. Gets the range of valid values for the specified field. |
pack-priv static Instant | |
private void | |
private long | |
public long | Returns: the number of milliseconds since the epoch of 1970-01-01T00:00:00ZConverts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z. |
public String | Returns: an ISO-8601 representation of this instant, not nullOverrides java. A string representation of this instant using ISO-8601 representation. |
public Instant | Returns: anInstant based on this instant with the time truncated, not nullthe unit to truncate to, not null unit)Returns a copy of this |
public long | Returns: the amount of time between this instant and the end instantthe end date, exclusive, which is converted to an endExclusive, TemporalUnit Instant , not nullthe unit to measure the amount in, not null unit)Implements java. Calculates the amount of time until another instant in terms of the specified unit. |
public Duration | |
public Instant | Returns: anInstant based on this with the adjustment made, not nullthe adjuster to use, not null adjuster)Overrides default java. Returns an adjusted copy of this instant. |
public Instant | Returns: anInstant based on this with the specified field set, not nullthe field to set in the result, not null field, long the new value of the field in the result newValue)Implements java. Returns a copy of this instant with the specified field set to a new value. |
pack-priv void | |
private Object | Returns: the instance ofSer , not nullWrites the object using a dedicated serialized form. |