0
0
JavaHow-ToBeginner · 4 min read

How to Use ZonedDateTime in Java: Syntax and Examples

Use ZonedDateTime in Java to represent date and time with a time zone. You can create it using methods like now() or of(), and manipulate or format it easily with built-in methods.
📐

Syntax

The ZonedDateTime class combines date, time, and time zone information. You can create an instance using:

  • ZonedDateTime.now() - current date and time with system default zone
  • ZonedDateTime.of() - specify date, time, and zone manually
  • ZonedDateTime.parse() - parse from a string

Common methods include plusDays(), minusHours(), and withZoneSameInstant() to adjust time or zone.

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

ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime specific = ZonedDateTime.of(2024, 6, 1, 12, 30, 0, 0, ZoneId.of("Europe/Paris"));
ZonedDateTime parsed = ZonedDateTime.parse("2024-06-01T12:30:00+02:00[Europe/Paris]");
💻

Example

This example shows how to create a ZonedDateTime for the current moment, change its time zone, and format it for display.

java
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // Current date and time with system default zone
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println("Current ZonedDateTime: " + now);

        // Change time zone to New York
        ZonedDateTime nyTime = now.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println("New York time: " + nyTime);

        // Format ZonedDateTime
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z VV");
        String formatted = nyTime.format(formatter);
        System.out.println("Formatted New York time: " + formatted);
    }
}
Output
Current ZonedDateTime: 2024-06-01T15:45:30.123+02:00[Europe/Paris] New York time: 2024-06-01T09:45:30.123-04:00[America/New_York] Formatted New York time: 2024-06-01 09:45:30 EDT America/New_York
⚠️

Common Pitfalls

Common mistakes when using ZonedDateTime include:

  • Confusing withZoneSameInstant() and withZoneSameLocal(): The first adjusts the instant to a new zone, the second keeps local time but changes the zone, which can cause unexpected results.
  • Parsing strings without zone info leads to errors or wrong zones.
  • Using LocalDateTime when you need zone info causes loss of time zone context.
java
import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimePitfall {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Paris"));

        // Wrong: changes zone but keeps local time (wrong instant)
        ZonedDateTime wrong = zdt.withZoneSameLocal(ZoneId.of("America/New_York"));
        System.out.println("Wrong zone change: " + wrong);

        // Right: changes zone and instant correctly
        ZonedDateTime right = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println("Right zone change: " + right);
    }
}
Output
Wrong zone change: 2024-06-01T15:45:30.123-04:00[America/New_York] Right zone change: 2024-06-01T09:45:30.123-04:00[America/New_York]
📊

Quick Reference

MethodDescription
ZonedDateTime.now()Get current date and time with system default zone
ZonedDateTime.of(..., ZoneId)Create ZonedDateTime with specific date, time, and zone
ZonedDateTime.parse(String)Parse ZonedDateTime from string with zone info
withZoneSameInstant(ZoneId)Change zone keeping the same instant in time
withZoneSameLocal(ZoneId)Change zone keeping the same local date-time (not recommended)
plusDays(long)Add days to the date-time
minusHours(long)Subtract hours from the date-time
format(DateTimeFormatter)Format ZonedDateTime to string

Key Takeaways

ZonedDateTime stores date, time, and time zone together for accurate time handling.
Use withZoneSameInstant() to convert between zones without changing the actual moment in time.
Avoid withZoneSameLocal() unless you want to keep local time but change the zone, which can cause errors.
Create ZonedDateTime using now(), of(), or parse() depending on your needs.
Format ZonedDateTime with DateTimeFormatter for readable output.