The of(int year, int month, int day, int hour, int minute, int second, int nanosecond, ZoneOffset offset) method of the OffsetDateTime class in Java is used to create an instance of OffsetDateTime from the passed values of year, month, day, hour, minute, second, nanosecond and offset. In this method, the value of the year, month, day, hour, minute, second and nano-second is passed in integer format and the method returns date-time on the basis of these values.
Syntax:
Java
Java
public static OffsetDateTime of(int year,
int month,
int day,
int hour,
int minute,
int second,
int nanosecond,
ZoneOffset offset)
Parameters: This method accepts eight parameters.
- year - It is of Integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
- month - It is of Integer type and represents the month of the year. It varies from 1(JANUARY) to 12(DECEMBER).
- day - It is of Integer type and represents the day of the month. It varies from 1 to 31.
- hour - It is of Integer type and represents the hour of the day. It varies from 0 to 23.
- minute - It is of Integer type and represents the minute of the hour. It varies from 0 to 59.
- second - It is of Integer type and represents the second of the minute. It varies from 0 to 59.
- nanosecond - It is of Integer type and represents the nanosecond. It varies from 0 to 999999999.
- offset - It is of ZoneOffset type and represents the zone offset. It should not be null.
// Java program to demonstrate
// OffsetDateTime of() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(
String[] args)
{
// Create OffsetDateTime object
OffsetDateTime offsetdatetime
= OffsetDateTime.of(
2020, 5, 20, 9, 10, 40,
50000, ZoneOffset.UTC);
// Print date-time
System.out.println(
"DATE-TIME: "
+ offsetdatetime);
}
}
Output:
Program 2:
DATE-TIME: 2020-05-20T09:10:40.000050Z
// Java program to demonstrate
// OffsetDateTime of() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Create OffsetDateTime object
OffsetDateTime offsetdatetime
= OffsetDateTime.of(
2020, 5, 20, 9, 10, 40,
20000, ZoneOffset.ofHoursMinutes(
5, 30));
// Print date-time
System.out.println(
"DATE-TIME: "
+ offsetdatetime);
}
}
Output:
References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#of(int, int, int, int, int, int, int, java.time.ZoneOffset)DATE-TIME: 2020-05-20T09:10:40.000020+05:30