public final class Instant extends AbstractInstant implements ReadableInstant, java.io.Serializable
Instant
is an implementation of ReadableInstant
.
As with all instants, it represents an exact point on the time-line,
but limited to the precision of milliseconds. An Instant
should be used to represent a point in time irrespective of any other
factor, such as chronology or time zone.
Internally, the class holds one piece of data, the instant as milliseconds from the Java epoch of 1970-01-01T00:00:00Z.
For example, an Instant can be used to compare two DateTime
objects irrespective of chronology or time zone.
boolean sameInstant = dt1.toInstant().equals(dt2.toInstant());Note that the following code will also perform the same check:
boolean sameInstant = dt1.isEqual(dt2);
Instant is thread-safe and immutable.
Constructor and Description |
---|
Instant()
Constructs an instance set to the current system millisecond time.
|
Instant(long instant)
Constructs an instance set to the milliseconds from 1970-01-01T00:00:00Z.
|
Instant(java.lang.Object instant)
Constructs an instance from an Object that represents a datetime.
|
Modifier and Type | Method and Description |
---|---|
Chronology |
getChronology()
Gets the chronology of the instant, which is ISO in the UTC zone.
|
long |
getMillis()
Gets the milliseconds of the instant.
|
Instant |
minus(long duration)
Gets a copy of this instant with the specified duration taken away.
|
Instant |
minus(ReadableDuration duration)
Gets a copy of this instant with the specified duration taken away.
|
static Instant |
now()
Obtains an
Instant set to the current system millisecond time. |
static Instant |
parse(java.lang.String str)
Parses a
Instant from the specified string. |
static Instant |
parse(java.lang.String str,
DateTimeFormatter formatter)
Parses a
Instant from the specified string using a formatter. |
Instant |
plus(long duration)
Gets a copy of this instant with the specified duration added.
|
Instant |
plus(ReadableDuration duration)
Gets a copy of this instant with the specified duration added.
|
DateTime |
toDateTime()
Get this object as a DateTime using ISOChronology in the default zone.
|
DateTime |
toDateTimeISO()
Deprecated.
Use toDateTime() as it is identical
|
Instant |
toInstant()
Get this object as an Instant by returning
this . |
MutableDateTime |
toMutableDateTime()
Get this object as a MutableDateTime using ISOChronology in the default zone.
|
MutableDateTime |
toMutableDateTimeISO()
Deprecated.
Use toMutableDateTime() as it is identical
|
Instant |
withDurationAdded(long durationToAdd,
int scalar)
Gets a copy of this instant with the specified duration added.
|
Instant |
withDurationAdded(ReadableDuration durationToAdd,
int scalar)
Gets a copy of this instant with the specified duration added.
|
Instant |
withMillis(long newMillis)
Gets a copy of this instant with different millis.
|
compareTo, equals, get, get, getZone, hashCode, isAfter, isAfter, isAfterNow, isBefore, isBefore, isBeforeNow, isEqual, isEqual, isEqualNow, isSupported, toDate, toDateTime, toDateTime, toMutableDateTime, toMutableDateTime, toString, toString
equals, get, getZone, hashCode, isAfter, isBefore, isEqual, isSupported, toString
public Instant()
now()
public Instant(long instant)
instant
- the milliseconds from 1970-01-01T00:00:00Zpublic Instant(java.lang.Object instant)
The recognised object types are defined in ConverterManager
and
include String, Calendar and Date.
instant
- the datetime object, null means nowjava.lang.IllegalArgumentException
- if the instant is invalidpublic static Instant now()
Instant
set to the current system millisecond time.public static Instant parse(java.lang.String str)
Instant
from the specified string.
This uses ISODateTimeFormat.dateTimeParser()
.
str
- the string to parse, not nullpublic static Instant parse(java.lang.String str, DateTimeFormatter formatter)
Instant
from the specified string using a formatter.str
- the string to parse, not nullformatter
- the formatter to use, not nullpublic Instant toInstant()
this
.toInstant
in interface ReadableInstant
toInstant
in class AbstractInstant
this
public Instant withMillis(long newMillis)
The returned object will be either be a new Instant or this
.
newMillis
- the new millis, from 1970-01-01T00:00:00Zpublic Instant withDurationAdded(long durationToAdd, int scalar)
If the addition is zero, then this
is returned.
durationToAdd
- the duration to add to this onescalar
- the amount of times to add, such as -1 to subtract oncejava.lang.ArithmeticException
- if the new instant exceeds the capacity of a longpublic Instant withDurationAdded(ReadableDuration durationToAdd, int scalar)
If the addition is zero, then this
is returned.
durationToAdd
- the duration to add to this one, null means zeroscalar
- the amount of times to add, such as -1 to subtract oncejava.lang.ArithmeticException
- if the new instant exceeds the capacity of a longpublic Instant plus(long duration)
If the amount is zero or null, then this
is returned.
duration
- the duration to add to this onejava.lang.ArithmeticException
- if the new instant exceeds the capacity of a longpublic Instant plus(ReadableDuration duration)
If the amount is zero or null, then this
is returned.
duration
- the duration to add to this one, null means zerojava.lang.ArithmeticException
- if the new instant exceeds the capacity of a longpublic Instant minus(long duration)
If the amount is zero or null, then this
is returned.
duration
- the duration to reduce this instant byjava.lang.ArithmeticException
- if the new instant exceeds the capacity of a longpublic Instant minus(ReadableDuration duration)
If the amount is zero or null, then this
is returned.
duration
- the duration to reduce this instant byjava.lang.ArithmeticException
- if the new instant exceeds the capacity of a longpublic long getMillis()
getMillis
in interface ReadableInstant
public Chronology getChronology()
This method returns ISOChronology.getInstanceUTC()
which
corresponds to the definition of the Java epoch 1970-01-01T00:00:00Z.
getChronology
in interface ReadableInstant
public DateTime toDateTime()
This method returns a DateTime object in the default zone. This differs from the similarly named method on DateTime, DateMidnight or MutableDateTime which retains the time zone. The difference is because Instant really represents a time without a zone, thus calling this method we really have no zone to 'retain' and hence expect to switch to the default zone.
This method definition preserves compatibility with earlier versions of Joda-Time.
toDateTime
in class AbstractInstant
@Deprecated public DateTime toDateTimeISO()
toDateTime()
.
This method returns a DateTime object in the default zone. This differs from the similarly named method on DateTime, DateMidnight or MutableDateTime which retains the time zone. The difference is because Instant really represents a time without a zone, thus calling this method we really have no zone to 'retain' and hence expect to switch to the default zone.
This method is deprecated because it is a duplicate of toDateTime()
.
However, removing it would cause the superclass implementation to be used,
which would create silent bugs in any caller depending on this implementation.
As such, the method itself is not currently planned to be removed.
This method definition preserves compatibility with earlier versions of Joda-Time.
toDateTimeISO
in class AbstractInstant
public MutableDateTime toMutableDateTime()
This method returns a MutableDateTime object in the default zone. This differs from the similarly named method on DateTime, DateMidnight or MutableDateTime which retains the time zone. The difference is because Instant really represents a time without a zone, thus calling this method we really have no zone to 'retain' and hence expect to switch to the default zone.
This method definition preserves compatibility with earlier versions of Joda-Time.
toMutableDateTime
in class AbstractInstant
@Deprecated public MutableDateTime toMutableDateTimeISO()
toMutableDateTime()
.
This method returns a MutableDateTime object in the default zone. This differs from the similarly named method on DateTime, DateMidnight or MutableDateTime which retains the time zone. The difference is because Instant really represents a time without a zone, thus calling this method we really have no zone to 'retain' and hence expect to switch to the default zone.
This method is deprecated because it is a duplicate of toMutableDateTime()
.
However, removing it would cause the superclass implementation to be used,
which would create silent bugs in any caller depending on this implementation.
As such, the method itself is not currently planned to be removed.
This method definition preserves compatibility with earlier versions of Joda-Time.
toMutableDateTimeISO
in class AbstractInstant
"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"