Java - Date Time Instants

What is a Instant

An instant is a point representing a unique moment in time on a timeline.

An epoch is an instant on a timeline that is used as a reference point to measure other instants.

Java Date Time Instant

An object of the Instant class represents an instant on the timeline.

The time interval between two consecutive instants on the timeline is one nanosecond.

The timeline uses 1970-01-01T00:00:00Z as the epoch.

Instants after the epoch have positive values while instants before the epoch have negative values.

The instant at the epoch is assigned a zero value.

Create Instnat

There are different ways you can create an instance of the Instant class.

Using its now() method, you can get the current instant using the system default clock.

Instant i1 = Instant.now();

Obtain an instance of the Instant class using an amount of time in different units from the epoch.

The following code creates an Instant object to represent 19 seconds from the epoch, which represents 1970-01-01T00:00:19Z:

Demo

import java.time.Instant;

public class Main {
  public static void main(String[] args) {
    // An instant: 19 seconds from the epoch
    Instant i2 = Instant.ofEpochSecond(19);
    System.out.println(i2);/*from w w  w .j a v  a2 s  .  c  om*/

  }
}

Result