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:
- year – the year to represent, from MIN_YEAR to MAX_YEAR.
- month – the month-of-year to represent, from 1 (January) to 12 (December).
- dayOfMonth – the day-of-month to represent, from 1 to 31.
- hour – the hour-of-day to represent, from 0 to 23.
- minute – the minute-of-hour to represent, from 0 to 59.
- second – the second-of-minute to represent, from 0 to 59.
- nanoOfSecond – the nano-of-second to represent, from 0 to 999, 999, 999.
- zone – the time-zone, not null.
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); } } |
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:
- date – the local date
- time – the local time
- zone – the time-zone
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); } } |
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:
- localDateTime – the local date-time
- time – the local time
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); } } |
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)
Please Login to comment...