Getting current date and time with Clock

java.time.Clock provides access to the current instant, date and time using a time-zone.

Getting current date and time with Clock

The example uses java.time.Clock to get the current date-time.
import java.time.Clock;
import java.time.Instant;

public class JavaCurrentDateTimeClock {

    public static void main(String[] args) {

        Clock clock = Clock.systemDefaultZone();

        Instant now = clock.instant();
        System.out.println(now);
    }
}
Output:
2019-07-14T06:11:01.116Z
The Clock.systemDefaultZone() method obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.
Clock clock = Clock.systemDefaultZone();

Comments