Java : Instant with Examples

Instant (Java SE 18 & JDK 18) with Examples.
You will find code examples on most Instant methods.


Summary

An instantaneous point on the time-line.

Class diagram

final Instant instant = Instant.ofEpochSecond(0);

System.out.println(instant); // 1970-01-01T00:00:00Z
System.out.println(instant.getEpochSecond()); // 0
final Instant instant = Instant.ofEpochSecond(1609849845L);

System.out.println(instant); // 2021-01-05T12:30:45Z
System.out.println(instant.getEpochSecond()); // 1609849845

Fields

static final Instant EPOCH

Constant for the 1970-01-01T00:00:00Z epoch instant.

System.out.println(Instant.EPOCH); // 1970-01-01T00:00:00Z

static final Instant MAX

The maximum supported Instant, '1000000000-12-31T23:59:59.999999999Z'.

System.out.println(Instant.MAX); // +1000000000-12-31T23:59:59.999999999Z

static final Instant MIN

The minimum supported Instant, '-1000000000-01-01T00:00Z'.

System.out.println(Instant.MIN); // -1000000000-01-01T00:00:00Z

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have this instant.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

{
    final var dateTime = OffsetDateTime.of(2021, 2, 28, 12, 30, 0, 0, ZoneOffset.ofHours(4));
    System.out.println(dateTime); // 2021-02-28T12:30+04:00

    System.out.println(instant.adjustInto(dateTime)); // 2000-01-01T04:00+04:00
}
{
    final var dateTime = ZonedDateTime.of(2021, 3, 4, 0, 0, 0, 0, ZoneId.systemDefault());
    System.out.println(dateTime); // 2021-03-04T00:00-08:00[America/Los_Angeles]

    System.out.println(instant.adjustInto(dateTime)); // 1999-12-31T16:00-08:00[America/Los_Angeles]
}
{
    final var dateTime = LocalDateTime.of(1999, 1, 1, 12, 30);
    System.out.println(dateTime); // 1999-01-01T12:30

    //instant.adjustInto(dateTime); // UnsupportedTemporalTypeException
}

OffsetDateTime atOffset (ZoneOffset offset)

Combines this instant with an offset to create an OffsetDateTime.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var ret1 = instant.atOffset(ZoneOffset.UTC);
System.out.println(ret1); // 2000-01-01T00:00Z

final var ret2 = instant.atOffset(ZoneOffset.ofHours(9));
System.out.println(ret2); // 2000-01-01T09:00+09:00

final var ret3 = instant.atOffset(ZoneOffset.ofHoursMinutes(-4, -30));
System.out.println(ret3); // 1999-12-31T19:30-04:30

ZonedDateTime atZone (ZoneId zone)

Combines this instant with a time-zone to create a ZonedDateTime.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var ret1 = instant.atZone(ZoneId.systemDefault());
System.out.println(ret1); // 1999-12-31T16:00-08:00[America/Los_Angeles]

final var ret2 = instant.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println(ret2); // 2000-01-01T09:00+09:00[Asia/Tokyo]

final var ret3 = instant.atZone(ZoneOffset.UTC);
System.out.println(ret3); // 2000-01-01T00:00Z

int compareTo (Instant otherInstant)

Compares this instant to the specified instant.

final var instant1 = Instant.ofEpochSecond(946684800L);
final var instant2 = Instant.ofEpochSecond(946684800L);

System.out.println(instant1); // 2000-01-01T00:00:00Z
System.out.println(instant2); // 2000-01-01T00:00:00Z
System.out.println(instant1.compareTo(instant2)); // 0
final var instant1 = Instant.ofEpochSecond(946684800L);
final var instant2 = Instant.ofEpochSecond(946684801L);

System.out.println(instant1); // 2000-01-01T00:00:00Z
System.out.println(instant2); // 2000-01-01T00:00:01Z
System.out.println(instant1.compareTo(instant2)); // -1
final var instant1 = Instant.ofEpochMilli(0L);
final var instant2 = Instant.ofEpochMilli(-1L);

System.out.println(instant1); // 1970-01-01T00:00:00Z
System.out.println(instant2); // 1969-12-31T23:59:59.999Z
System.out.println(instant1.compareTo(instant2)); // 1
System.out.println(Instant.MIN); // -1000000000-01-01T00:00:00Z
System.out.println(Instant.MAX); // +1000000000-12-31T23:59:59.999999999Z
System.out.println(Instant.MIN.compareTo(Instant.MAX)); // -1

boolean equals (Object other)

Checks if this instant is equal to the specified instant.

final var instant1 = Instant.ofEpochSecond(946684800L);
final var instant2 = Instant.ofEpochSecond(946684800L);

System.out.println(instant1); // 2000-01-01T00:00:00Z
System.out.println(instant2); // 2000-01-01T00:00:00Z
System.out.println(instant1.equals(instant2)); // true
final var instant1 = Instant.ofEpochSecond(946684800L);
final var instant2 = Instant.ofEpochSecond(946684801L);

System.out.println(instant1); // 2000-01-01T00:00:00Z
System.out.println(instant2); // 2000-01-01T00:00:01Z
System.out.println(instant1.equals(instant2)); // false
final var instant1 = Instant.ofEpochMilli(0L);
final var instant2 = Instant.ofEpochMilli(-1L);

System.out.println(instant1); // 1970-01-01T00:00:00Z
System.out.println(instant2); // 1969-12-31T23:59:59.999Z
System.out.println(instant1.equals(instant2)); // false
System.out.println(Instant.MIN); // -1000000000-01-01T00:00:00Z
System.out.println(Instant.MAX); // +1000000000-12-31T23:59:59.999999999Z
System.out.println(Instant.MIN.equals(Instant.MAX)); // false

static Instant from (TemporalAccessor temporal)

Obtains an instance of Instant from a temporal object.

final var temporal = ZonedDateTime.of(1999, 1, 1, 12, 30, 0, 0, ZoneOffset.UTC);
System.out.println(temporal); // 1999-01-01T12:30Z

System.out.println(Instant.from(temporal)); // 1999-01-01T12:30:00Z
final var temporal = ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault());
System.out.println(temporal); // 2000-01-01T00:00-08:00[America/Los_Angeles]

System.out.println(Instant.from(temporal)); // 2000-01-01T08:00:00Z
final var temporal = OffsetDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(4));
System.out.println(temporal); // 2000-01-01T00:00+04:00

System.out.println(Instant.from(temporal)); // 1999-12-31T20:00:00Z
final var temporal = LocalDateTime.of(2000, 1, 1, 0, 0);
System.out.println(temporal); // 2000-01-01T00:00

//Instant.from(temporal); // DateTimeException

int get (TemporalField field)

Gets the value of the specified field from this instant as an int.

final var instant = Instant.ofEpochMilli(946684800123L);
System.out.println(instant); // 2000-01-01T00:00:00.123Z

//instant.get(ChronoField.INSTANT_SECONDS); // UnsupportedTemporalTypeException

System.out.println(instant.get(ChronoField.MILLI_OF_SECOND)); // 123
System.out.println(instant.get(ChronoField.MICRO_OF_SECOND)); // 123000
System.out.println(instant.get(ChronoField.NANO_OF_SECOND)); // 123000000

long getEpochSecond ()

Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.getEpochSecond()); // 946684800
final var dateTime = ZonedDateTime.of(1999, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
System.out.println(dateTime); // 1999-01-01T00:00Z

final var instant = dateTime.toInstant();
System.out.println(instant.getEpochSecond()); // 915148800
System.out.println(Instant.MIN.getEpochSecond()); // -31557014167219200
System.out.println(Instant.EPOCH.getEpochSecond()); // 0
System.out.println(Instant.MAX.getEpochSecond()); // 31556889864403199

long getLong (TemporalField field)

Gets the value of the specified field from this instant as a long.

final var instant = Instant.ofEpochMilli(946684800123L);
System.out.println(instant); // 2000-01-01T00:00:00.123Z

System.out.println(instant.getLong(ChronoField.INSTANT_SECONDS)); // 946684800

System.out.println(instant.getLong(ChronoField.MILLI_OF_SECOND)); // 123
System.out.println(instant.getLong(ChronoField.MICRO_OF_SECOND)); // 123000
System.out.println(instant.getLong(ChronoField.NANO_OF_SECOND)); // 123000000

int getNano ()

Gets the number of nanoseconds, later along the time-line, from the start of the second.

final var instant = Instant.ofEpochSecond(946684800L, 123456789L);
System.out.println(instant); // 2000-01-01T00:00:00.123456789Z

System.out.println(instant.getNano()); // 123456789
final var instant = Instant.ofEpochMilli(946684800123L);
System.out.println(instant); // 2000-01-01T00:00:00.123Z

System.out.println(instant.getNano()); // 123000000

int hashCode ()

Returns a hash code for this instant.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z
System.out.println(instant.hashCode()); // 946684800

System.out.println(Instant.MIN.hashCode()); // -336857328
System.out.println(Instant.EPOCH.hashCode()); // 0
System.out.println(Instant.MAX.hashCode()); // -625237510

boolean isAfter (Instant otherInstant)

Checks if this instant is after the specified instant.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var instant2 = Instant.ofEpochSecond(946684800L - 1);
final var instant3 = Instant.ofEpochSecond(946684800L);
final var instant4 = Instant.ofEpochSecond(946684800L + 1);

System.out.println(instant2); // 1999-12-31T23:59:59Z
System.out.println(instant3); // 2000-01-01T00:00:00Z
System.out.println(instant4); // 2000-01-01T00:00:01Z

System.out.println(instant.isAfter(instant2)); // true
System.out.println(instant.isAfter(instant3)); // false
System.out.println(instant.isAfter(instant4)); // false
final var instant = Instant.ofEpochSecond(0, 0);
System.out.println(instant); // 1970-01-01T00:00:00Z

final var instant2 = Instant.ofEpochSecond(0, -1);
final var instant3 = Instant.ofEpochSecond(0, 0);
final var instant4 = Instant.ofEpochSecond(0, 1);

System.out.println(instant2); // 1969-12-31T23:59:59.999999999Z
System.out.println(instant3); // 1970-01-01T00:00:00Z
System.out.println(instant4); // 1970-01-01T00:00:00.000000001Z

System.out.println(instant.isAfter(instant2)); // true
System.out.println(instant.isAfter(instant3)); // false
System.out.println(instant.isAfter(instant4)); // false

boolean isBefore (Instant otherInstant)

Checks if this instant is before the specified instant.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var instant2 = Instant.ofEpochSecond(946684800L - 1);
final var instant3 = Instant.ofEpochSecond(946684800L);
final var instant4 = Instant.ofEpochSecond(946684800L + 1);

System.out.println(instant2); // 1999-12-31T23:59:59Z
System.out.println(instant3); // 2000-01-01T00:00:00Z
System.out.println(instant4); // 2000-01-01T00:00:01Z

System.out.println(instant.isBefore(instant2)); // false
System.out.println(instant.isBefore(instant3)); // false
System.out.println(instant.isBefore(instant4)); // true
final var instant = Instant.ofEpochSecond(0, 0);
System.out.println(instant); // 1970-01-01T00:00:00Z

final var instant2 = Instant.ofEpochSecond(0, -1);
final var instant3 = Instant.ofEpochSecond(0, 0);
final var instant4 = Instant.ofEpochSecond(0, 1);

System.out.println(instant2); // 1969-12-31T23:59:59.999999999Z
System.out.println(instant3); // 1970-01-01T00:00:00Z
System.out.println(instant4); // 1970-01-01T00:00:00.000000001Z

System.out.println(instant.isBefore(instant2)); // false
System.out.println(instant.isBefore(instant3)); // false
System.out.println(instant.isBefore(instant4)); // true

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.isSupported(ChronoField.INSTANT_SECONDS)); // true

System.out.println(instant.isSupported(ChronoField.MILLI_OF_SECOND)); // true
System.out.println(instant.isSupported(ChronoField.MICRO_OF_SECOND)); // true
System.out.println(instant.isSupported(ChronoField.NANO_OF_SECOND)); // true

System.out.println(instant.isSupported(ChronoField.NANO_OF_DAY)); // false
System.out.println(instant.isSupported(ChronoField.YEAR)); // false
System.out.println(instant.isSupported(ChronoField.HOUR_OF_DAY)); // false

boolean isSupported (TemporalUnit unit)

Checks if the specified unit is supported.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.isSupported(ChronoUnit.YEARS)); // false
System.out.println(instant.isSupported(ChronoUnit.MONTHS)); // false

System.out.println(instant.isSupported(ChronoUnit.DAYS)); // true
System.out.println(instant.isSupported(ChronoUnit.HOURS)); // true
System.out.println(instant.isSupported(ChronoUnit.MINUTES)); // true
System.out.println(instant.isSupported(ChronoUnit.SECONDS)); // true
System.out.println(instant.isSupported(ChronoUnit.MILLIS)); // true
System.out.println(instant.isSupported(ChronoUnit.MICROS)); // true
System.out.println(instant.isSupported(ChronoUnit.NANOS)); // true

Instant minus (long amountToSubtract, TemporalUnit unit)

Returns a copy of this instant with the specified amount subtracted.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minus(1, ChronoUnit.DAYS)); // 1999-12-31T00:00:00Z
System.out.println(instant.minus(2, ChronoUnit.HOURS)); // 1999-12-31T22:00:00Z
System.out.println(instant.minus(3, ChronoUnit.MINUTES)); // 1999-12-31T23:57:00Z
System.out.println(instant.minus(4, ChronoUnit.SECONDS)); // 1999-12-31T23:59:56Z
final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minus(1, ChronoUnit.MILLIS)); // 1999-12-31T23:59:59.999Z
System.out.println(instant.minus(2, ChronoUnit.MICROS)); // 1999-12-31T23:59:59.999998Z
System.out.println(instant.minus(3, ChronoUnit.NANOS)); // 1999-12-31T23:59:59.999999997Z

Instant minus (TemporalAmount amountToSubtract)

Returns a copy of this instant with the specified amount subtracted.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minus(Period.ofDays(1))); // 1999-12-31T00:00:00Z
System.out.println(instant.minus(Duration.ofHours(2))); // 1999-12-31T22:00:00Z
System.out.println(instant.minus(Duration.ofMinutes(3))); // 1999-12-31T23:57:00Z
System.out.println(instant.minus(Duration.ofSeconds(4))); // 1999-12-31T23:59:56Z
final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minus(Duration.ofMillis(1))); // 1999-12-31T23:59:59.999Z
System.out.println(instant.minus(Duration.ofNanos(2))); // 1999-12-31T23:59:59.999999998Z

Instant minusMillis (long millisToSubtract)

Returns a copy of this instant with the specified duration in milliseconds subtracted.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minusMillis(1)); // 1999-12-31T23:59:59.999Z
System.out.println(instant.minusMillis(2));  // 1999-12-31T23:59:59.998Z
System.out.println(instant.minusMillis(1000)); // 1999-12-31T23:59:59Z

Instant minusNanos (long nanosToSubtract)

Returns a copy of this instant with the specified duration in nanoseconds subtracted.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minusNanos(1)); // 1999-12-31T23:59:59.999999999Z
System.out.println(instant.minusNanos(2));  // 1999-12-31T23:59:59.999999998Z
System.out.println(instant.minusNanos(1000)); // 1999-12-31T23:59:59.999999Z
System.out.println(instant.minusNanos(1000 * 1000)); // 1999-12-31T23:59:59.999Z

Instant minusSeconds (long secondsToSubtract)

Returns a copy of this instant with the specified duration in seconds subtracted.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.minusSeconds(1)); // 1999-12-31T23:59:59Z
System.out.println(instant.minusSeconds(2));  // 1999-12-31T23:59:58Z
System.out.println(instant.minusSeconds(60)); // 1999-12-31T23:59:00Z

static Instant now ()

Obtains the current instant from the system clock.

final var now = Instant.now();
System.out.println(now); // 2022-01-23T05:49:38.795124400Z

System.out.println(ZonedDateTime.now()); // 2022-01-22T21:49:38.798123700-08:00[America/Los_Angeles]

static Instant now (Clock clock)

Obtains the current instant from the specified clock.

// Clock advanced for 5 days.
final var clock = Clock.offset(Clock.systemDefaultZone(), Duration.ofDays(5));

System.out.println(Instant.now()); // 2022-01-23T05:52:05.231401500Z
System.out.println(Instant.now(clock)); // 2022-01-28T05:52:05.233400Z

static Instant ofEpochMilli (long epochMilli)

Obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.

System.out.println(Instant.ofEpochMilli(0)); // 1970-01-01T00:00:00Z
System.out.println(Instant.ofEpochMilli(946684800000L)); // 2000-01-01T00:00:00Z
System.out.println(Instant.ofEpochMilli(946684800123L)); // 2000-01-01T00:00:00.123Z

static Instant ofEpochSecond (long epochSecond)

Obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z.

System.out.println(Instant.ofEpochSecond(0)); // 1970-01-01T00:00:00Z
System.out.println(Instant.ofEpochSecond(1)); // 1970-01-01T00:00:01Z
System.out.println(Instant.ofEpochSecond(-1)); // 1969-12-31T23:59:59Z
System.out.println(Instant.ofEpochSecond(60)); // 1970-01-01T00:01:00Z
System.out.println(Instant.ofEpochSecond(60 * 60 * 24)); // 1970-01-02T00:00:00Z

System.out.println(Instant.ofEpochSecond(946684800L)); // 2000-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.

System.out.println(Instant.ofEpochSecond(0, 0)); // 1970-01-01T00:00:00Z
System.out.println(Instant.ofEpochSecond(0, 123)); // 1970-01-01T00:00:00.000000123Z
System.out.println(Instant.ofEpochSecond(946684830L, 123456789L)); // 2000-01-01T00:00:30.123456789Z

static Instant parse (CharSequence text)

Obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.

System.out.println(Instant.parse("2011-12-03T10:15:30Z")); // 2011-12-03T10:15:30Z
System.out.println(Instant.parse("1999-01-01T00:00:00.123456789Z")); // 1999-01-01T00:00:00.123456789Z
System.out.println(Instant.parse("2021-05-01T00:00:00+09:00")); // 2021-04-30T15:00:00Z

//Instant.parse("2021/5/1 00:00"); // DateTimeParseException

Instant plus (long amountToAdd, TemporalUnit unit)

Returns a copy of this instant with the specified amount added.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plus(1, ChronoUnit.DAYS)); // 2000-01-02T00:00:00Z
System.out.println(instant.plus(2, ChronoUnit.HOURS)); // 2000-01-01T02:00:00Z
System.out.println(instant.plus(3, ChronoUnit.MINUTES)); // 2000-01-01T00:03:00Z
System.out.println(instant.plus(4, ChronoUnit.SECONDS)); // 2000-01-01T00:00:04Z
final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plus(1, ChronoUnit.MILLIS)); // 2000-01-01T00:00:00.001Z
System.out.println(instant.plus(2, ChronoUnit.MICROS)); // 2000-01-01T00:00:00.000002Z
System.out.println(instant.plus(3, ChronoUnit.NANOS)); // 2000-01-01T00:00:00.000000003Z

Instant plus (TemporalAmount amountToAdd)

Returns a copy of this instant with the specified amount added.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plus(Period.ofDays(1))); // 2000-01-02T00:00:00Z
System.out.println(instant.plus(Duration.ofHours(2))); // 2000-01-01T02:00:00Z
System.out.println(instant.plus(Duration.ofMinutes(3))); // 2000-01-01T00:03:00Z
System.out.println(instant.plus(Duration.ofSeconds(4))); // 2000-01-01T00:00:04Z
final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plus(Duration.ofMillis(1))); // 2000-01-01T00:00:00.001Z
System.out.println(instant.plus(Duration.ofNanos(2))); // 2000-01-01T00:00:00.000000002Z

Instant plusMillis (long millisToAdd)

Returns a copy of this instant with the specified duration in milliseconds added.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plusMillis(1)); // 2000-01-01T00:00:00.001Z
System.out.println(instant.plusMillis(2));  // 2000-01-01T00:00:00.002Z
System.out.println(instant.plusMillis(1000)); // 2000-01-01T00:00:01Z

Instant plusNanos (long nanosToAdd)

Returns a copy of this instant with the specified duration in nanoseconds added.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plusNanos(1)); // 2000-01-01T00:00:00.000000001Z
System.out.println(instant.plusNanos(2));  // 2000-01-01T00:00:00.000000002Z
System.out.println(instant.plusNanos(1000)); // 2000-01-01T00:00:00.000001Z
System.out.println(instant.plusNanos(1000 * 1000)); // 2000-01-01T00:00:00.001Z

Instant plusSeconds (long secondsToAdd)

Returns a copy of this instant with the specified duration in seconds added.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.plusSeconds(1)); // 2000-01-01T00:00:01Z
System.out.println(instant.plusSeconds(2));  // 2000-01-01T00:00:02Z
System.out.println(instant.plusSeconds(60)); // 2000-01-01T00:01:00Z

<R> R query (TemporalQuery<R> query)

Queries this instant using the specified query.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.query(TemporalQueries.precision())); // Nanos

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

final var ret1 = instant.range(ChronoField.INSTANT_SECONDS);
System.out.println(ret1); // -9223372036854775808 - 9223372036854775807

final var ret2 = instant.range(ChronoField.MILLI_OF_SECOND);
System.out.println(ret2); // 0 - 999

final var ret3 = instant.range(ChronoField.MICRO_OF_SECOND);
System.out.println(ret3); // 0 - 999999

final var ret4 = instant.range(ChronoField.NANO_OF_SECOND);
System.out.println(ret4); // 0 - 999999999

long toEpochMilli ()

Converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.

System.out.println(Instant.EPOCH); // 1970-01-01T00:00:00Z

System.out.println(Instant.EPOCH.toEpochMilli()); // 0
final var instant = Instant.ofEpochMilli(946684800999L);
System.out.println(instant); // 2000-01-01T00:00:00.999Z

System.out.println(instant.toEpochMilli()); // 946684800999
final var instant = Instant.ofEpochSecond(946684800L, 123456789L);
System.out.println(instant); // 2000-01-01T00:00:00.123456789Z

System.out.println(instant.toEpochMilli()); // 946684800123

String toString ()

A string representation of this instant using ISO-8601 representation.

final var ret1 = Instant.EPOCH.toString();
System.out.println(ret1); // 1970-01-01T00:00:00Z

final var ret2 = Instant.ofEpochSecond(946684800L).toString();
System.out.println(ret2); // 2000-01-01T00:00:00Z

final var ret3 = Instant.ofEpochSecond(946684800L, 123456789L).toString();
System.out.println(ret3); // 2000-01-01T00:00:00.123456789Z

Instant truncatedTo (TemporalUnit unit)

Returns a copy of this Instant truncated to the specified unit.

final var instant = Instant.ofEpochSecond(949548645L, 123456789L);
System.out.println(instant); // 2000-02-03T03:30:45.123456789Z

System.out.println(instant.truncatedTo(ChronoUnit.DAYS)); // 2000-02-03T00:00:00Z
System.out.println(instant.truncatedTo(ChronoUnit.HOURS)); // 2000-02-03T03:00:00Z
System.out.println(instant.truncatedTo(ChronoUnit.MINUTES)); // 2000-02-03T03:30:00Z
System.out.println(instant.truncatedTo(ChronoUnit.SECONDS)); // 2000-02-03T03:30:45Z
System.out.println(instant.truncatedTo(ChronoUnit.MILLIS)); // 2000-02-03T03:30:45.123Z
System.out.println(instant.truncatedTo(ChronoUnit.MICROS)); // 2000-02-03T03:30:45.123456Z
System.out.println(instant.truncatedTo(ChronoUnit.NANOS)); // 2000-02-03T03:30:45.123456789Z

//instant.truncatedTo(ChronoUnit.YEARS); // UnsupportedTemporalTypeException
//instant.truncatedTo(ChronoUnit.MONTHS); // UnsupportedTemporalTypeException

long until (Temporal endExclusive, TemporalUnit unit)

Calculates the amount of time until another instant in terms of the specified unit.

final var instant1 = Instant.ofEpochSecond(0);
final var instant2 = Instant.ofEpochSecond(24 * 60 * 60);
System.out.println(instant1); // 1970-01-01T00:00:00Z
System.out.println(instant2); // 1970-01-02T00:00:00Z

System.out.println(instant1.until(instant2, ChronoUnit.DAYS)); // 1
System.out.println(instant1.until(instant2, ChronoUnit.HOURS)); // 24
System.out.println(instant1.until(instant2, ChronoUnit.MINUTES)); // 1440
System.out.println(instant1.until(instant2, ChronoUnit.SECONDS)); // 86400
System.out.println(instant1.until(instant2, ChronoUnit.NANOS)); // 86400000000000

Instant with (TemporalAdjuster adjuster)

Returns an adjusted copy of this instant.

final var instant = Instant.ofEpochSecond(100000000);
System.out.println(instant); // 1973-03-03T09:46:40Z

final var adjuster = new TemporalAdjuster() {
    @Override
    public Temporal adjustInto(Temporal temporal) {
        final var epoch = temporal.getLong(ChronoField.INSTANT_SECONDS);
        return Instant.ofEpochSecond(epoch * 5);
    }
};

final var ret = instant.with(adjuster);
System.out.println(ret); // 1985-11-05T00:53:20Z
System.out.println(ret.getEpochSecond()); // 500000000

Instant with (TemporalField field, long newValue)

Returns a copy of this instant with the specified field set to a new value.

final var instant = Instant.ofEpochSecond(946684800L);
System.out.println(instant); // 2000-01-01T00:00:00Z

System.out.println(instant.with(ChronoField.MILLI_OF_SECOND, 123)); // 2000-01-01T00:00:00.123Z
System.out.println(instant.with(ChronoField.MICRO_OF_SECOND, 123)); // 2000-01-01T00:00:00.000123Z
System.out.println(instant.with(ChronoField.NANO_OF_SECOND, 123)); // 2000-01-01T00:00:00.000000123Z
final var instant = Instant.ofEpochSecond(946684800L, 123);
System.out.println(instant); // 2000-01-01T00:00:00.000000123Z

System.out.println(instant.with(ChronoField.INSTANT_SECONDS, 0)); // 1970-01-01T00:00:00.000000123Z

Related posts

To top of page