Open In App

OffsetTime of() method in Java with Examples

Last Updated : 05 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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.

Return value: This method always returns the OffsetTime.

Exception: This method throws DateTimeException if any parameter value is out of the range.

Below programs illustrate the of() method of OffsetTime class in Java:

Program 1:




// 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:

TIME: 08:20:40.000050Z

Program 2:




// 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:

TIME: 05:40:30.000020-18:00

Program 3:




// 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:

TIME: 06:10:20.000030+18:00

References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetTime.html#of(int, int, int, int, java.time.ZoneOffset)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads