Course contentsShow
← Java
Lesson 1848 of 2,87038. Date and Time API (java.time)Free lesson

Creating ZonedDateTime Instances

Using ZonedDateTime.now(), of(), and from() methods to create zoned date-time objects.

Creating ZonedDateTime Instances

What you'll learn: How to create ZonedDateTime objects using three essential factory methods: now(), of(), and from().

The Core Idea

A ZonedDateTime combines a date, time, and time zone—but first you need to create one. Java provides three primary ways to instantiate ZonedDateTime objects, each suited for different scenarios.

The Three Creation Methods

1. ZonedDateTime.now()

Captures the current moment in a specific time zone:

ZonedDateTime currentMoment = ZonedDateTime.now();  // System default zone
ZonedDateTime tokyoNow = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));

2. ZonedDateTime.of()

Builds a ZonedDateTime from specific date-time components:

// Using separate components
ZonedDateTime meeting = ZonedDateTime.of(
    2024, 3, 15,     // year, month, day
    14, 30, 0, 0,    // hour, minute, second, nano
    ZoneId.of("America/New_York")
);

// Combining LocalDateTime with ZoneId
LocalDateTime dateTime = LocalDateTime.of(2024, 3, 15, 14, 30);
ZonedDateTime zoned = ZonedDateTime.of(dateTime, ZoneId.of("Europe/Paris"));

3. ZonedDateTime.from()

Converts another temporal object into a ZonedDateTime:

Instant instant = Instant.now();
ZonedDateTime fromInstant = ZonedDateTime.from(instant.atZone(ZoneId.of("UTC")));

When to Use Each

  • now(): Timestamping events, logging current time
  • of(): Scheduling future events, creating specific moments
  • from(): Converting between temporal types with zone information

Key Takeaway: Use ZonedDateTime.now() for current timestamps, of() to construct specific date-times with zones, and from() to convert compatible temporal objects—all producing immutable, zone-aware instances.