Open In App

ZonedDateTime of() Method in Java with Examples

In ZonedDateTime class, there are three types of() method depending upon the parameters passed to it.

of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone)

of() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a year, month, day, hour, minute, second, nanosecond and time-zone.all these values year, month, day, hour, minute, second, nanosecond and time-zone are passed as parameter.



Syntax:

public static ZonedDateTime of(int year, int month, 
                               int dayOfMonth,
                               int hour, int minute,
                               int second, int nanoOfSecond,
                               ZoneId zone)

Parameters: This method accepts eight different parameters:



Return value: This method returns the offset date-time.

Exception: This method throws DateTimeException if the value of any field is out of range, or if the day-of-month is invalid for the month-year.

Below programs illustrate the of() method:
Program 1:




// Java program to demonstrate
// ZonedDateTime.of() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ZonedDateTime object
        // using of method
        ZonedDateTime lt
            = ZonedDateTime.of(
                2020, 12, 3, 12, 20, 59,
                90000, ZoneId.systemDefault());
  
        // print result
        System.out.println("ZonedDateTime : "
                           + lt);
    }
}

Output:
ZonedDateTime : 2020-12-03T12:20:59.000090Z[Etc/UTC]

of(LocalDate date, LocalTime time, ZoneId zone)

of(Clock clock) method of a ZonedDateTime class used to return an instance of ZonedDateTime from a local date, time and zone.all these localdate, localtime and zone are passed as parameter.

Syntax:

public static ZonedDateTime of(LocalDate date,
                               LocalTime time,
                               ZoneId zone)

Parameters: This method accepts three different parameter:

Return value: This method returns the offset date-time.

Below programs illustrate the of() method:
Program 1:




// Java program to demonstrate
// ZonedDateTime.of() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime
        LocalDate localdate
            = LocalDate.parse("2020-12-30");
  
        // create a LocalTime
        LocalTime localtime
            = LocalTime.parse("17:52:49");
  
        // create a zoneid
        ZoneId zid
            = ZoneId.of("Europe/Paris");
  
        // create an ZonedDateTime object
        // using of() method
        ZonedDateTime zt
            = ZonedDateTime.of(localdate,
                               localtime, zid);
  
        // print result
        System.out.println("ZonedDateTime : "
                           + zt);
    }
}

Output:
ZonedDateTime : 2020-12-30T17:52:49+01:00[Europe/Paris]

of(LocalDateTime localDateTime, ZoneId zone)

of() method of a ZonedDateTime class used to return an instance of ZonedDateTime from a localdatetime and zone.all these localdatetime and zone are passed as parameter.

Syntax:

public static ZonedDateTime of(LocalDateTime localDateTime,
                               ZoneId zone)

Parameters: This method accepts two different parameter:

Return value: This method returns the offset date-time.

Below programs illustrate the of() method:
Program 1:




// Java program to demonstrate
// ZonedDateTime.of() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDateTime
        LocalDateTime local
            = LocalDateTime
                  .parse("2018-11-03T12:45:30");
  
        // create a zoneid
        ZoneId zid = ZoneId.of("Europe/Paris");
  
        // create an ZonedDateTime object
        // using of()
        ZonedDateTime zt
            = ZonedDateTime.of(
                local, zid);
  
        // print result
        System.out.println("ZonedDateTime : "
                           + zt);
    }
}

Output:
ZonedDateTime : 2018-11-03T12:45:30+01:00[Europe/Paris]

References:
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#of(int, int, int, int, int, int, int, java.time.ZoneId)
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#of(java.time.LocalDate, java.time.LocalTime, java.time.ZoneId)
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#of(java.time.LocalDateTime, java.time.ZoneId)


Article Tags :