Clock systemDefaultZone() Method in Java with Examples
The systemDefaultZone()
method is a static method of the java.time.Clock
class in Java. It returns the system default time zone that the clock uses to convert instant to date and time values.
Syntax:
public static Clock systemDefaultZone()
Example 1:
import java.time.Clock;
import java.time.ZoneId;
public class ClockExample {
public static void main(String[] args) {
Clock clock = Clock.systemDefaultZone();
ZoneId zoneId = clock.getZone();
System.out.println("System Default Time Zone: " + zoneId);
}
}
Output:
System Default Time Zone: Asia/Kolkata
Example 2:
import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class ClockExample {
public static void main(String[] args) {
Clock clock = Clock.systemDefaultZone();
LocalDateTime now = LocalDateTime.now(clock);
System.out.println("Current Date and Time: " + now);
}
}
Output:
Current Date and Time: 2021-10-20T14:30:45.123456
In the above example, we have used the systemDefaultZone()
method to get the system default time zone and then used it to get the current date and time using the LocalDateTime.now(clock)
method.