The of(int hour, int minute, int second, int nanosecond, ZoneOffset offset) method of the OffsetTime class in Java is used to create an instance of OffsetTime from the passed values of hour, minute, second and nanosecond. In this method, we pass the value of hour, minute, second and nanosecond in integer form and it returns time on basis of these values as OffsetTime.
Syntax:
Java
Java
Java
public static OffsetTime of(int hour,
int minute,
int second,
int nanosecond,
ZoneOffset offset)
Parameters: The method accepts five parameters:
- hour - It represents the hour of the day. It varies from 0 to 23.
- minute - It represents the minute of the hour. It varies from 0 to 59.
- second - It represents the second of the minute. It varies from 0 to 59.
- nanosecond - It represents the nano of the second. It varies from 0 to 999999999.
- offset - It represents the zone offset. It should not be null.
// Java program to demonstrate
// OffsetTime of() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Create OffsetTime object
OffsetTime offsettime
= OffsetTime.of(
8, 20, 40, 50000,
ZoneOffset.UTC);
// Print time
System.out.println(
"TIME: "
+ offsettime);
}
}
Output:
Program 2:
TIME: 08:20:40.000050Z
// Java program to demonstrate
// OffsetTime of() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Create OffsetTime object
OffsetTime offsettime
= OffsetTime.of(
5, 40, 30, 20000,
ZoneOffset.MIN);
// Print time
System.out.println("TIME: "
+ offsettime);
}
}
Output:
Program 3:
TIME: 05:40:30.000020-18:00
// Java program to demonstrate
// OffsetTime of() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Create OffsetTime object
OffsetTime offsettime
= OffsetTime.of(
6, 10, 20, 30000,
ZoneOffset.MAX);
// Print time
System.out.println("TIME: "
+ offsettime);
}
}
Output:
References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetTime.html#of(int, int, int, int, java.time.ZoneOffset)TIME: 06:10:20.000030+18:00