Java ZonedDateTime.now() method with Examples

Tags: Java ZonedDateTime Java 8

In this Java core tutorial we learn how to use the ZonedDateTime.now() method to get a ZonedDateTime object which represents the current date and time in the default time zone or a given specified time zone.

Table of contents

  1. Get Current Date Time from System Clock in Default Time Zone
  2. Get Current Date Time from System Clock in Specified Time Zone
  3. Get Current Date Time from Specified Clock

Get Current Date Time from System Clock in Default Time Zone

In Java we can use the ZonedDateTime.now() method to get ZonedDateTime object from the system clock in the default time zone as the following Java code.

ZonedDateTimeNowExample1.java

import java.time.ZonedDateTime;

public class ZonedDateTimeNowExample1 {
    public static void main(String... args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        System.out.println(zonedDateTime);
    }
}
The output as below.
2022-04-25T21:57:41.617297400+07:00[Asia/Bangkok]

Get Current Date Time from System Clock in Specified Time Zone

In Java we can use the ZonedDateTime.now(ZoneId zone) method to get ZonedDateTime object from the system clock in the the specified time zone.

For example, in the following Java program we use the ZonedDateTime.now(ZoneId zone) method to get ZonedDateTime in Australia/Sydney time zone.

ZonedDateTimeNowExample2.java

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeNowExample2 {
    public static void main(String... args) {
        ZoneId zoneId = ZoneId.of("Australia/Sydney");

        ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);

        System.out.println(zonedDateTime);
    }
}
The output as below.
2022-04-26T00:58:05.324277400+10:00[Australia/Sydney]

Below is more Java example code of how to use ZonedDateTime.now(ZoneId zone) method.

ZonedDateTimeNowExample3.java

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeNowExample3 {
    public static void main(String... args) {
        ZoneId zoneId1 = ZoneId.systemDefault();
        ZoneId zoneId2 = ZoneId.of("Europe/Paris");
        ZoneId zoneId3 = ZoneId.of("Asia/Tokyo");

        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(zoneId1);
        ZonedDateTime zonedDateTime2 = ZonedDateTime.now(zoneId2);
        ZonedDateTime zonedDateTime3 = ZonedDateTime.now(zoneId3);

        System.out.println(zonedDateTime1);
        System.out.println(zonedDateTime2);
        System.out.println(zonedDateTime3);
    }
}
The output as below.
2022-04-25T21:58:30.105022800+07:00[Asia/Bangkok]
2022-04-25T16:58:30.105022800+02:00[Europe/Paris]
2022-04-25T23:58:30.107017500+09:00[Asia/Tokyo]

Get Current Date Time from Specified Clock

In Java we can use the ZonedDateTime.now(Clock clock) method to get ZonedDateTime object from the specified clock.

In the following Java example code we use the ZonedDateTime.now(Clock clock) method with system clock in the UTC zone to get ZonedDateTime object in UTC time zone.

ZonedDateTimeNowExample4.java

import java.time.Clock;
import java.time.ZonedDateTime;

public class ZonedDateTimeNowExample4 {
    public static void main(String... args) {
        Clock clock = Clock.systemUTC();
        ZonedDateTime zonedDateTime = ZonedDateTime.now(clock);

        System.out.println(zonedDateTime);
    }
}
The output as below.
2022-04-25T14:58:52.141811100Z

Below is more Java example codes of how to use ZonedDateTime.now(Clock clock) method.

ZonedDateTimeNowExample5.java

import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeNowExample5 {
    public static void main(String... args) {
        Clock clock1 = Clock.systemDefaultZone();
        Clock clock2 = Clock.system(ZoneId.of("America/Los_Angeles"));

        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(clock1);
        ZonedDateTime zonedDateTime2 = ZonedDateTime.now(clock2);

        System.out.println(zonedDateTime1);
        System.out.println(zonedDateTime2);
    }
}
The output as below.
2022-04-25T21:59:15.582258900+07:00[Asia/Bangkok]
2022-04-25T07:59:15.582258900-07:00[America/Los_Angeles]

Happy Coding 😊

Java ZonedDateTime.of() Method with Examples

Java ZonedDateTime.ofLocal() Method with Examples

Java ZonedDateTime.ofInstant() Method with Examples

Java ZonedDateTime.ofStrict() Method with Examples

Java ZonedDateTime.parse() Method with Examples

Java ZonedDateTime.getOffset() Method with Examples

Java ZonedDateTime.withEarlierOffsetAtOverlap() Method with Examples

Java ZonedDateTime.withLaterOffsetAtOverlap() Method with Examples

Java ZonedDateTime.getZone() Method with Examples

Java ZonedDateTime.withZoneSameLocal() Method with Examples

Java ZonedDateTime.withZoneSameInstant() Method with Examples

Java ZonedDateTime.withFixedOffsetZone() Method with Examples

Java ZonedDateTime.toLocalDateTime() Method with Examples

Java ZonedDateTime.toLocalDate() Method with Examples

Java ZonedDateTime.toLocalTime() Method with Examples

Java ZonedDateTime.getYear() Method with Examples

Java ZonedDateTime.getMonthValue() Method with Examples

Java ZonedDateTime.getMonth() Method with Examples

Java ZonedDateTime.getDayOfMonth() Method with Examples

Java ZonedDateTime.getDayOfYear() Method with Examples

Java ZonedDateTime.getDayOfWeek() Method with Examples

Java ZonedDateTime.getHour() Method with Examples

Java ZonedDateTime.getMinute() Method with Examples

Java ZonedDateTime.getSecond() Method with Examples

Java ZonedDateTime.getNano() Method with Examples

Java ZonedDateTime.withYear() Method with Examples

Java ZonedDateTime.withMonth() Method with Examples

Java ZonedDateTime.withDayOfMonth() Method with Examples

Java ZonedDateTime.withDayOfYear() Method with Examples

Java ZonedDateTime.withHour() Method with Examples

Java ZonedDateTime.withMinute() Method with Examples

Java ZonedDateTime.withSecond() Method with Examples

Java ZonedDateTime.withNano() Method with Examples

Java ZonedDateTime.plusYears() Method with Examples

Java ZonedDateTime.plusMonths() Method with Examples

Java ZonedDateTime.plusWeeks() Method with Examples

Java ZonedDateTime.plusDays() Method with Examples

Java ZonedDateTime.plusHours() Method with Examples

Java ZonedDateTime.plusMinutes() Method with Examples

Java ZonedDateTime.plusSeconds() Method with Examples

Java ZonedDateTime.plusNanos() Method with Examples

Java ZonedDateTime.minusYears() Method with Examples

Java ZonedDateTime.minusMonths() Method with Examples

Java ZonedDateTime.minusWeeks() Method with Examples

Java ZonedDateTime.minusDays() Method with Examples

Java ZonedDateTime.minusHours() Method with Examples

Java ZonedDateTime.minusMinutes() Method with Examples

Java ZonedDateTime.minusSeconds() Method with Examples

Java ZonedDateTime.minusNanos() Method with Examples

Java ZonedDateTime.format() Method with Examples

Java ZonedDateTime.toOffsetDateTime() Method with Examples

Java ZonedDateTime.equals() Method with Examples

Java ZonedDateTime.toString() Method with Examples

Java Compare two ZonedDateTime Values

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDate to ZonedDateTime