java.time.OffsetDateTime.of() Method Example



Description

The java.time.OffsetDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) method Obtains an instance of OffsetDateTime from a year, month, day, hour, minute, second, nanosecond and offset.

Declaration

Following is the declaration for java.time.OffsetDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset) method.

public static OffsetDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneOffset offset)

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

  • offset − the zone offset, not null

Return Value

the offset 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.OffsetDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second) method.

package com.tutorialspoint;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeDemo {
   public static void main(String[] args) {
 
      OffsetDateTime date = OffsetDateTime.of(2017,2,3,6,30,40,50000,ZoneOffset.UTC);
      System.out.println(date);  
   }
}

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

2017-02-03T06:30:40.000050Z
Advertisements