HomeLanguagesJavaOffsetTime of() method in Java with Examples

OffsetTime of() method in Java with Examples

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)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32653 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7028 POSTS0 COMMENTS
Nicole Veronica
12166 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12257 POSTS0 COMMENTS
Shaida Kate Naidoo
7159 POSTS0 COMMENTS
Ted Musemwa
7423 POSTS0 COMMENTS
Thapelo Manthata
7119 POSTS0 COMMENTS
Umr Jansen
7121 POSTS0 COMMENTS