-
Notifications
You must be signed in to change notification settings - Fork 4
Getting current date and time with Clock
Ramesh Fadatare edited this page Jul 14, 2019
·
2 revisions
java.time.Clock provides access to the current instant, date and time using a time-zone.
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();