java.time.ZonedDateTime.of() Method Example



Description

The java.time.ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone) method obtains an instance of ZonedDateTime from year, month, day, hour, minute, second, nanosecond and time-zone.

Declaration

Following is the declaration for java.time.ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone) method.

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

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

the zoned date-time, not null.

Exceptions

DateTimeException − if the value of any field is out of range, or if the day-of-month is invalid for the month-year.

Example

The following example shows the usage of java.time.ZonedDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second) method.

package com.tutorialspoint;

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

public class ZonedDateTimeDemo {
   public static void main(String[] args) {
 
      ZonedDateTime date = ZonedDateTime.of(2017,2,3,6,30,40,50000, ZoneId.systemDefault());
      System.out.println(date);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-02-03T06:30:40.000050+05:30[Asia/Calcutta]
Advertisements