public final class Instant extends java.lang.Object implements CalendricalObject, java.lang.Comparable<Instant>, java.io.Serializable
This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.
For practicality, the instant is stored with some constraints.
The measurable time-line is restricted to the number of seconds that can be held
in a long
. This is greater than the current estimated age of the universe.
The instant is stored to nanosecond resolution.
The range of an instant requires the storage of a number larger than a long
.
The standard storage scheme uses epoch-seconds 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. The standard storage stores the fraction
of a second as nanosecond-of-second which will always be between 0 and 999,999,999.
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 with a simplification. The Java time-scale is defined as follows:
In 2012, the definition of the Java time-scale is the same as UTC for all days except those where a leap-second occurs. On days where a leap-second does occur, the time-scale effectively eliminates the leap-second, maintaining the fiction of 86400 seconds in the day.
The main benefit of always dividing the day into 86400 subdivisions is that it matches the expectations of most users of the API. The alternative is to force every user to understand what a leap second is and to force them to have special logic to handle them. Most applications do not have access to a clock that is accurate enough to record leap-seconds. Most applications also do not have a problem with a second being a very small amount longer or shorter than a real SI second during a leap-second.
If an application does have access to an accurate clock that reports leap-seconds, then the recommended technique to implement the Java time-scale is to use the UTC-SLS convention. UTC-SLS effectively smoothes the leap-second over the last 1000 seconds of the day, making each of the last 1000 "seconds" 1/1000th longer or shorter than a real SI second.
One final problem is the definition of the agreed international civil time before the
introduction of modern UTC in 1972. This includes the Java epoch of 1970-01-01
.
It is intended that instants before 1972 be interpreted based on the solar day divided
into 86400 subdivisions.
The Java time-scale is used for all date-time classes supplied by JSR-310.
This includes Instant
, LocalDate
, LocalTime
, OffsetDateTime
,
ZonedDateTime
and Duration
.
Modifier and Type | Field and Description |
---|---|
static Instant |
EPOCH
Constant for the 1970-01-01T00:00:00Z epoch instant.
|
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Instant otherInstant)
Compares this instant to the specified instant.
|
boolean |
equals(java.lang.Object otherInstant)
Checks if this instant is equal to the specified instant.
|
<R> R |
extract(java.lang.Class<R> type)
Extracts date-time information in a generic way.
|
static Instant |
from(CalendricalObject calendrical)
Obtains an instance of
Instant from a calendrical. |
long |
getEpochSecond()
Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
|
int |
getNanoOfSecond()
Gets the number of nanoseconds, later along the time-line, from the start
of the second.
|
int |
hashCode()
Returns a hash code for this instant.
|
boolean |
isAfter(Instant otherInstant)
Checks if this instant is after the specified instant.
|
boolean |
isBefore(Instant otherInstant)
Checks if this instant is before the specified instant.
|
Instant |
minus(Duration duration)
Returns a copy of this instant with the specified duration subtracted.
|
Instant |
minus(long amount,
java.util.concurrent.TimeUnit unit)
Returns a copy of this duration with the specified duration subtracted.
|
Instant |
minusMillis(long millisToSubtract)
Returns a copy of this instant with the specified duration in milliseconds subtracted.
|
Instant |
minusNanos(long nanosToSubtract)
Returns a copy of this instant with the specified duration in nanoseconds subtracted.
|
Instant |
minusSeconds(long secondsToSubtract)
Returns a copy of this instant with the specified duration in seconds subtracted.
|
static Instant |
now()
Obtains the current instant from the system clock.
|
static Instant |
now(Clock clock)
Obtains the current instant from the specified clock.
|
static Instant |
ofEpochMilli(long epochMilli)
Obtains an instance of
Instant using milliseconds from the
epoch of 1970-01-01T00:00:00Z. |
static Instant |
ofEpochNano(java.math.BigInteger epochNano)
Obtains an instance of
Instant using nanoseconds from the
epoch of 1970-01-01T00:00:00Z. |
static Instant |
ofEpochNano(long epochNano)
Obtains an instance of
Instant using nanoseconds from the
epoch of 1970-01-01T00:00:00Z. |
static Instant |
ofEpochSecond(java.math.BigDecimal epochSecond)
Obtains an instance of
Instant using seconds from the
epoch of 1970-01-01T00:00:00Z. |
static Instant |
ofEpochSecond(long epochSecond)
Obtains an instance of
Instant using seconds from the
epoch of 1970-01-01T00:00:00Z. |
static Instant |
ofEpochSecond(long epochSecond,
long nanoAdjustment)
Obtains an instance of
Instant using seconds from the
epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second. |
Instant |
plus(Duration duration)
Returns a copy of this instant with the specified duration added.
|
Instant |
plus(long amount,
java.util.concurrent.TimeUnit unit)
Returns a copy of this duration with the specified duration added.
|
Instant |
plusMillis(long millisToAdd)
Returns a copy of this instant with the specified duration in milliseconds added.
|
Instant |
plusNanos(long nanosToAdd)
Returns a copy of this instant with the specified duration in nanoseconds added.
|
Instant |
plusSeconds(long secondsToAdd)
Returns a copy of this instant with the specified duration in seconds added.
|
long |
toEpochMilli()
Converts this instant to the number of milliseconds from the epoch
of 1970-01-01T00:00:00Z.
|
java.math.BigInteger |
toEpochNano()
Converts this instant to the number of nanoseconds from the epoch
of 1970-01-01T00:00:00Z expressed as a
BigInteger . |
java.math.BigDecimal |
toEpochSecond()
Converts this instant to the number of seconds from the epoch
of 1970-01-01T00:00:00Z expressed as a
BigDecimal . |
java.lang.String |
toString()
A string representation of this instant using ISO-8601 representation.
|
Instant |
with(CalendricalAdjuster adjuster)
Returns a copy of this calendrical with the specified calendrical set.
|
public static final Instant EPOCH
public static Instant now()
This will query the system UTC clock
to
obtain the current instant.
Using this method will prevent the ability to use an alternate time-source for testing because the clock is effectively hard-coded.
public static Instant now(Clock clock)
This will query the specified clock to obtain the current time.
Using this method allows the use of an alternate clock for testing.
The alternate clock may be introduced using dependency injection
.
clock
- the clock to use, not nullpublic static Instant ofEpochSecond(long epochSecond)
Instant
using seconds from the
epoch of 1970-01-01T00:00:00Z.
The nanosecond field is set to zero.
epochSecond
- the number of seconds from 1970-01-01T00:00:00Zpublic static Instant ofEpochSecond(long epochSecond, long nanoAdjustment)
Instant
using seconds from the
epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
This method allows an arbitrary number of nanoseconds to be passed in. The factory will alter the values of the second and nanosecond in order to ensure that the stored nanosecond is in the range 0 to 999,999,999. For example, the following will result in the exactly the same instant:
Instant.ofSeconds(3, 1); Instant.ofSeconds(4, -999999999); Instant.ofSeconds(2, 1000000001);
epochSecond
- the number of seconds from 1970-01-01T00:00:00ZnanoAdjustment
- the nanosecond adjustment to the number of seconds, positive or negativejava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic static Instant ofEpochSecond(java.math.BigDecimal epochSecond)
Instant
using seconds from the
epoch of 1970-01-01T00:00:00Z.
The seconds and nanoseconds are extracted from the specified BigDecimal
.
If the decimal is larger than Long.MAX_VALUE
or has more than 9 decimal
places then an exception is thrown.
epochSecond
- the number of seconds from 1970-01-01T00:00:00Z, up to scale 9java.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic static Instant ofEpochMilli(long epochMilli)
Instant
using milliseconds from the
epoch of 1970-01-01T00:00:00Z.
The seconds and nanoseconds are extracted from the specified milliseconds.
epochMilli
- the number of milliseconds from 1970-01-01T00:00:00Zpublic static Instant ofEpochNano(long epochNano)
Instant
using nanoseconds from the
epoch of 1970-01-01T00:00:00Z.
The seconds and nanoseconds are extracted from the specified nanoseconds.
epochNano
- the number of nanoseconds from 1970-01-01T00:00:00Zpublic static Instant ofEpochNano(java.math.BigInteger epochNano)
Instant
using nanoseconds from the
epoch of 1970-01-01T00:00:00Z.
The seconds and nanoseconds are extracted from the specified BigInteger
.
If the resulting seconds value is larger than Long.MAX_VALUE
then an
exception is thrown.
epochNano
- the number of nanoseconds from 1970-01-01T00:00:00Z, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic static Instant from(CalendricalObject calendrical)
Instant
from a calendrical.
A calendrical represents some form of date and time information.
This factory converts the arbitrary calendrical to an instance of Instant
.
calendrical
- the calendrical to convert, not nullCalendricalException
- if unable to convert to an Instant
public long getEpochSecond()
The epoch second count is a simple incrementing count of seconds where
second 0 is 1970-01-01T00:00:00Z.
The nanosecond part of the day is returned by getNanosOfSecond
.
public int getNanoOfSecond()
The nanosecond-of-second value measures the total number of nanoseconds from
the second returned by getEpochSecond
.
public Instant plus(Duration duration)
This instance is immutable and unaffected by this method call.
duration
- the duration to add, positive or negative, not nullInstant
based on this instant with the specified duration added, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant plus(long amount, java.util.concurrent.TimeUnit unit)
The duration to be added is measured in terms of the specified unit.
This instance is immutable and unaffected by this method call.
amount
- the duration to add, positive or negativeunit
- the unit that the duration is measured in, not nullInstant
based on this duration with the specified duration added, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant plusSeconds(long secondsToAdd)
This instance is immutable and unaffected by this method call.
secondsToAdd
- the seconds to add, positive or negativeInstant
based on this instant with the specified seconds added, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant plusMillis(long millisToAdd)
This instance is immutable and unaffected by this method call.
millisToAdd
- the milliseconds to add, positive or negativeInstant
based on this instant with the specified milliseconds added, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant plusNanos(long nanosToAdd)
This instance is immutable and unaffected by this method call.
nanosToAdd
- the nanoseconds to add, positive or negativeInstant
based on this instant with the specified nanoseconds added, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant minus(Duration duration)
This instance is immutable and unaffected by this method call.
duration
- the duration to subtract, positive or negative, not nullInstant
based on this instant with the specified duration subtracted, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant minus(long amount, java.util.concurrent.TimeUnit unit)
The duration to be subtracted is measured in terms of the specified unit.
This instance is immutable and unaffected by this method call.
amount
- the duration to subtract, positive or negativeunit
- the unit that the duration is measured in, not nullDuration
based on this duration with the specified duration subtracted, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant minusSeconds(long secondsToSubtract)
This instance is immutable and unaffected by this method call.
secondsToSubtract
- the seconds to subtract, positive or negativeInstant
based on this instant with the specified seconds subtracted, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant minusMillis(long millisToSubtract)
This instance is immutable and unaffected by this method call.
millisToSubtract
- the milliseconds to subtract, positive or negativeInstant
based on this instant with the specified milliseconds subtracted, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic Instant minusNanos(long nanosToSubtract)
This instance is immutable and unaffected by this method call.
nanosToSubtract
- the nanoseconds to subtract, positive or negativeInstant
based on this instant with the specified nanoseconds subtracted, not nulljava.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic <R> R extract(java.lang.Class<R> type)
This method exists to fulfill the CalendricalObject
interface.
This implementation returns the following types:
Instant
extract
in interface CalendricalObject
R
- the type to extracttype
- the type to extract, null returns nullpublic Instant with(CalendricalAdjuster adjuster)
CalendricalObject
This allows the manipulation of a calendrical in a general way.
For example, if this calendrical is a LocalDateTime
and the specified calendrical
is a LocalDate
, then the implementation will return a new LocalDateTime
with the date changed.
An implementation must handle the following types if possible:
with
in interface CalendricalObject
adjuster
- the calendrical to set into this calendrical, null returns nullpublic java.math.BigDecimal toEpochSecond()
BigDecimal
.public java.math.BigInteger toEpochNano()
BigInteger
.public long toEpochMilli()
If this instant represents a point on the time-line too far in the future
or past to fit in a long
milliseconds, then an exception is thrown.
If this instant has greater than millisecond precision, then the conversion will drop any excess precision information as though the amount in nanoseconds was subject to integer division by one million.
java.lang.ArithmeticException
- if the calculation exceeds the supported rangepublic int compareTo(Instant otherInstant)
The comparison is based on the time-line position of the instants.
compareTo
in interface java.lang.Comparable<Instant>
otherInstant
- the other instant to compare to, not nulljava.lang.NullPointerException
- if otherInstant is nullpublic boolean isAfter(Instant otherInstant)
The comparison is based on the time-line position of the instants.
otherInstant
- the other instant to compare to, not nulljava.lang.NullPointerException
- if otherInstant is nullpublic boolean isBefore(Instant otherInstant)
The comparison is based on the time-line position of the instants.
otherInstant
- the other instant to compare to, not nulljava.lang.NullPointerException
- if otherInstant is nullpublic boolean equals(java.lang.Object otherInstant)
The comparison is based on the time-line position of the instants.
equals
in class java.lang.Object
otherInstant
- the other instant, null returns falsepublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
The format of the returned string will be yyyy-MM-ddTHH:mm:ss.SSSSSSSSSZ
.
toString
in class java.lang.Object