Java ZonedDateTime of()

In this guide, you will learn about the ZonedDateTime of() method in Java programming and how to use it with an example.

1. ZonedDateTime of() Method Overview

Definition:

The of() method of the ZonedDateTime class in Java is a static factory method that is used to obtain an instance of ZonedDateTime from a set of date and time fields. This method offers various overloaded versions, allowing the creation of ZonedDateTime instances in different manners.

Syntax:

1. ZonedDateTime.of(LocalDateTime dateTime, ZoneId zone)
2. ZonedDateTime.of(LocalDate date, LocalTime time, ZoneId zone)
3. ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)

Parameters:

- dateTime: The local date-time instance.

- date: The local date instance.

- time: The local time instance.

- zone: The time-zone, which may be an offset or a region.

- year: The year to represent.

- month: The month-of-year to represent.

- dayOfMonth: The day-of-month to represent.

- hour: The hour-of-day to represent.

- minute: The minute-of-hour to represent.

- second: The second-of-minute to represent, by default it is 0.

- nanoOfSecond: The nanosecond-of-second to represent, by default it is 0.

Key Points:

- The of() method is overloaded, providing flexibility in creating ZonedDateTime instances.

- The method combines the provided date and time fields and associates them with the specified time zone.

- If the specified local date time is invalid due to the time-zone offset change, the method adjusts the date time as per the transition rules.

2. ZonedDateTime of() Method Example

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZonedDateTimeOfExample {
    public static void main(String[] args) {
        // Example 1: Using of(LocalDateTime dateTime, ZoneId zone)
        LocalDateTime dateTime = LocalDateTime.of(2023, 9, 20, 12, 34, 56);
        ZoneId zone = ZoneId.of("Europe/Paris");
        ZonedDateTime zonedDateTime1 = ZonedDateTime.of(dateTime, zone);
        System.out.println("ZonedDateTime1: " + zonedDateTime1);

        // Example 2: Using of(LocalDate date, LocalTime time, ZoneId zone)
        LocalDate date = LocalDate.of(2023, 9, 20);
        LocalTime time = LocalTime.of(12, 34, 56);
        ZonedDateTime zonedDateTime2 = ZonedDateTime.of(date, time, zone);
        System.out.println("ZonedDateTime2: " + zonedDateTime2);

        // Example 3: Using of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)
        ZonedDateTime zonedDateTime3 = ZonedDateTime.of(2023, 9, 20, 12, 34, 56, 0, zone);
        System.out.println("ZonedDateTime3: " + zonedDateTime3);
    }
}

Output:

ZonedDateTime1: 2023-09-20T12:34:56+02:00[Europe/Paris]
ZonedDateTime2: 2023-09-20T12:34:56+02:00[Europe/Paris]
ZonedDateTime3: 2023-09-20T12:34:56+02:00[Europe/Paris]

Explanation:

In this example, we used the three overloaded versions of the ZonedDateTime.of() method to create ZonedDateTime instances. 

The first version takes a LocalDateTime and a ZoneId, the second takes a LocalDate, a LocalTime, and a ZoneId, and the third version takes the individual date and time components along with a ZoneId

All versions return a ZonedDateTime instance associated with the specified time zone.

Comments