Skip to content

Getting current date and time with Instant example

Ramesh Fadatare edited this page Jul 11, 2019 · 1 revision

java.time.Instant models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.

Getting current date and time with Instant example

The code example uses java.time.Instant to get the current date and time.

import java.time.Instant;

public class JavaCurrentDateInstant {

    public static void main(String[] args) {
        
        Instant instant = Instant.now();
        System.out.println(instant);
    }
}

Output:

2019-07-11T17:26:19.881Z

The Instant.now() method obtains the current instant from the system clock:

Instant instant = Instant.now();
Clone this wiki locally