Java : Instant with Examples

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


Summary

An instantaneous point on the time-line.

Class diagram

final var instant = Instant.ofEpochSecond(0);

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

System.out.println(instant); // 2100-01-01T00:00:00Z
System.out.println(instant.getEpochSecond()); // 4102444800

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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

{
    final var zone = ZoneId.systemDefault();
    System.out.println(zone); // America/Los_Angeles

    final var dateTime = ZonedDateTime.of(2150, 2, 3, 12, 30, 59, 0, zone);
    System.out.println(dateTime); // 2150-02-03T12:30:59-08:00[America/Los_Angeles]

    System.out.println(instant.adjustInto(dateTime)); // 2099-12-31T16:00-08:00[America/Los_Angeles]
}
{
    final var offset = ZoneOffset.ofHours(9);
    System.out.println(offset); // +09:00

    final var dateTime = OffsetDateTime.of(2150, 2, 3, 12, 30, 59, 0, offset);
    System.out.println(dateTime); // 2150-02-03T12:30:59+09:00

    System.out.println(instant.adjustInto(dateTime)); // 2100-01-01T09:00+09:00
}
final var instant = Instant.ofEpochSecond(0);
System.out.println(instant); // 1970-01-01T00:00:00Z

final var dateTime = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(dateTime); // 2100-01-01T00:00

try {
    final var ret = instant.adjustInto(dateTime);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: InstantSeconds

OffsetDateTime atOffset (ZoneOffset offset)

Combines this instant with an offset to create an OffsetDateTime.

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

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

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

final var ret3 = instant.atOffset(ZoneOffset.ofHoursMinutes(-4, -30));
System.out.println(ret3); // 2099-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

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

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

int compareTo (Instant otherInstant)

Compares this instant to the specified instant.

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

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

System.out.println(instant1); // 2100-01-01T00:00:00Z
System.out.println(instant2); // 2100-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(4102444800L);
final var instant2 = Instant.ofEpochSecond(4102444800L);

System.out.println(instant1); // 2100-01-01T00:00:00Z
System.out.println(instant2); // 2100-01-01T00:00:00Z
System.out.println(instant1.equals(instant2)); // true
final var instant1 = Instant.ofEpochSecond(4102444800L);
final var instant2 = Instant.ofEpochSecond(4102444800L + 1);

System.out.println(instant1); // 2100-01-01T00:00:00Z
System.out.println(instant2); // 2100-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 zone = ZoneId.systemDefault();
System.out.println(zone); // America/Los_Angeles

final var temporal = ZonedDateTime.of(2100, 1, 1, 0, 0, 0, 0, zone);
System.out.println(temporal); // 2100-01-01T00:00-08:00[America/Los_Angeles]

System.out.println(Instant.from(temporal)); // 2100-01-01T08:00:00Z
final var zone = ZoneOffset.UTC;
System.out.println(zone); // Z

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

System.out.println(Instant.from(temporal)); // 2100-01-01T12:30:00Z
final var offset = ZoneOffset.ofHours(-4);
System.out.println(offset); // -04:00

final var temporal = OffsetDateTime.of(2100, 1, 1, 0, 0, 0, 0, offset);
System.out.println(temporal); // 2100-01-01T00:00-04:00

System.out.println(Instant.from(temporal)); // 2100-01-01T04:00:00Z
final var temporal = LocalDateTime.of(2100, 1, 1, 0, 0);
System.out.println(temporal); // 2100-01-01T00:00

try {
    final var ret = Instant.from(temporal);
} catch (DateTimeException e) {
    System.out.println("DateTimeException! : " + e.getMessage());
}

// Result
// ↓
//DateTimeException! : Unable to obtain Instant from TemporalAccessor:
// 2100-01-01T00:00 of type java.time.LocalDateTime

int get (TemporalField field)

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

Please see also : getLong(TemporalField field)

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

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

try {
    final var ret = instant.get(ChronoField.INSTANT_SECONDS);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: InstantSeconds

long getEpochSecond ()

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

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

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

final var instant = dateTime.toInstant();
System.out.println(instant.getEpochSecond()); // 4070908800
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(4102444800123L);
System.out.println(instant); // 2100-01-01T00:00:00.123Z

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

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(4102444800L, 123456789L);
System.out.println(instant); // 2100-01-01T00:00:00.123456789Z

System.out.println(instant.getNano()); // 123456789
final var instant = Instant.ofEpochMilli(4102444800123L);
System.out.println(instant); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z
System.out.println(instant.hashCode()); // -192522496

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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

System.out.println(instant2); // 2099-12-31T23:59:59Z
System.out.println(instant3); // 2100-01-01T00:00:00Z
System.out.println(instant4); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

System.out.println(instant2); // 2099-12-31T23:59:59Z
System.out.println(instant3); // 2100-01-01T00:00:00Z
System.out.println(instant4); // 2100-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(4102444800L);
System.out.println(instant); // 2100-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(4102444800L);
System.out.println(instant); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

System.out.println(instant.minus(1, ChronoUnit.MILLIS)); // 2099-12-31T23:59:59.999Z
System.out.println(instant.minus(2, ChronoUnit.MICROS)); // 2099-12-31T23:59:59.999998Z
System.out.println(instant.minus(3, ChronoUnit.NANOS)); // 2099-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

System.out.println(instant.minus(Duration.ofMillis(1))); // 2099-12-31T23:59:59.999Z
System.out.println(instant.minus(Duration.ofNanos(2))); // 2099-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

System.out.println(instant.minusMillis(1)); // 2099-12-31T23:59:59.999Z
System.out.println(instant.minusMillis(2)); // 2099-12-31T23:59:59.998Z
System.out.println(instant.minusMillis(1000)); // 2099-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

System.out.println(instant.minusNanos(1)); // 2099-12-31T23:59:59.999999999Z
System.out.println(instant.minusNanos(2)); // 2099-12-31T23:59:59.999999998Z
System.out.println(instant.minusNanos(1000)); // 2099-12-31T23:59:59.999999Z
System.out.println(instant.minusNanos(1000 * 1000)); // 2099-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

static Instant now ()

Obtains the current instant from the system clock.

final var now = Instant.now();
System.out.println(now); // 2024-05-04T07:50:20.071519600Z

static Instant now (Clock clock)

Obtains the current instant from the specified clock.

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

System.out.println(Instant.now()); // 2024-05-04T07:53:47.517010100Z
System.out.println(Instant.now(clock)); // 2024-05-09T07:53:47.520005100Z

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(4102444800000L)); // 2100-01-01T00:00:00Z
System.out.println(Instant.ofEpochMilli(4102444800123L)); // 2100-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(4102444800L)); // 2100-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.

final var instant1 = Instant.ofEpochSecond(0, 0);
System.out.println(instant1); // 1970-01-01T00:00:00Z

final var instant2 = Instant.ofEpochSecond(0, 123);
System.out.println(instant2); // 1970-01-01T00:00:00.000000123Z

final var instant3 = Instant.ofEpochSecond(4102444800L, 123456789L);
System.out.println(instant3); // 2100-01-01T00:00:00.123456789Z

static Instant parse (CharSequence text)

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

final var instant1 = Instant.parse("2100-12-03T10:15:30Z");
System.out.println(instant1); // 2100-12-03T10:15:30Z

final var instant2 = Instant.parse("1970-01-01T00:00:00.123456789Z");
System.out.println(instant2); // 1970-01-01T00:00:00.123456789Z

final var instant3 = Instant.parse("2100-05-01T00:00:00+09:00");
System.out.println(instant3); // 2100-04-30T15:00:00Z
try {
    final var ret = Instant.parse("2100/5/1 00:00");
} catch (DateTimeParseException e) {
    System.out.println("DateTimeParseException! : " + e.getMessage());
}

// Result
// ↓
//DateTimeParseException! : Text '2100/5/1 00:00' could not be parsed at index 4

Instant plus (long amountToAdd, TemporalUnit unit)

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

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

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

System.out.println(instant.plus(1, ChronoUnit.MILLIS)); // 2100-01-01T00:00:00.001Z
System.out.println(instant.plus(2, ChronoUnit.MICROS)); // 2100-01-01T00:00:00.000002Z
System.out.println(instant.plus(3, ChronoUnit.NANOS)); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

System.out.println(instant.plus(Duration.ofMillis(1))); // 2100-01-01T00:00:00.001Z
System.out.println(instant.plus(Duration.ofNanos(2))); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

System.out.println(instant.plusMillis(1)); // 2100-01-01T00:00:00.001Z
System.out.println(instant.plusMillis(2)); // 2100-01-01T00:00:00.002Z
System.out.println(instant.plusMillis(1000)); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

System.out.println(instant.plusNanos(1)); // 2100-01-01T00:00:00.000000001Z
System.out.println(instant.plusNanos(2)); // 2100-01-01T00:00:00.000000002Z
System.out.println(instant.plusNanos(1000)); // 2100-01-01T00:00:00.000001Z
System.out.println(instant.plusNanos(1000 * 1000)); // 2100-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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

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

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

Queries this instant using the specified query.

final var instant = Instant.ofEpochSecond(4102444800L);
System.out.println(instant); // 2100-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(4102444800L);
System.out.println(instant); // 2100-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(4102444800999L);
System.out.println(instant); // 2100-01-01T00:00:00.999Z

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

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

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(4102444800L).toString();
System.out.println(ret2); // 2100-01-01T00:00:00Z

final var ret3 = Instant.ofEpochSecond(4102444800L, 123456789L).toString();
System.out.println(ret3); // 2100-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(4102544459L, 123456789L);
System.out.println(instant); // 2100-01-02T03:40:59.123456789Z

System.out.println(instant.truncatedTo(ChronoUnit.DAYS)); // 2100-01-02T00:00:00Z
System.out.println(instant.truncatedTo(ChronoUnit.HOURS)); // 2100-01-02T03:00:00Z
System.out.println(instant.truncatedTo(ChronoUnit.MINUTES)); // 2100-01-02T03:40:00Z
System.out.println(instant.truncatedTo(ChronoUnit.SECONDS)); // 2100-01-02T03:40:59Z
System.out.println(instant.truncatedTo(ChronoUnit.MILLIS)); // 2100-01-02T03:40:59.123Z
System.out.println(instant.truncatedTo(ChronoUnit.MICROS)); // 2100-01-02T03:40:59.123456Z
System.out.println(instant.truncatedTo(ChronoUnit.NANOS)); // 2100-01-02T03:40:59.123456789Z

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(4102444800L);
System.out.println(instant); // 2100-01-01T00:00:00Z

final var ret1 = instant.with(ChronoField.MILLI_OF_SECOND, 123);
System.out.println(ret1); // 2100-01-01T00:00:00.123Z

final var ret2 = instant.with(ChronoField.MICRO_OF_SECOND, 123);
System.out.println(ret2); // 2100-01-01T00:00:00.000123Z

final var ret3 = instant.with(ChronoField.NANO_OF_SECOND, 123);
System.out.println(ret3); // 2100-01-01T00:00:00.000000123Z
final var instant = Instant.ofEpochSecond(4102444800L, 123);
System.out.println(instant); // 2100-01-01T00:00:00.000000123Z

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

Related posts

To top of page