Wednesday, July 3, 2024
HomeLanguagesJavaOffsetTime of(LocalTime) method in Java with Examples

OffsetTime of(LocalTime) method in Java with Examples

The of(LocalTime time, ZoneOffset offset) method of the OffsetTime class in Java is used to create an instance of OffsetTime from the given instances of localtime and offset.

Syntax:

public static OffsetTime of(LocalTime time,
                            ZoneOffset offset)

Parameters: The method accepts two parameters.

  • time – It represents the local time. It should not be null.
  • offset – It represents the zone offset. It should not be null.

Return value: This method returns the OffsetTime.

Exception: This method does not throw any exception.

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

Program 1:




// Java program to demonstrate
// OffsetTime of(
// LocalTime, ZoneOffset) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(
        String[] args)
    {
        // Create OffsetTime object
        OffsetTime offsettime
            = OffsetTime.of(
                LocalTime.now(),
                ZoneOffset.UTC);
  
        // Print time
        System.out.println(
            "TIME: "
            + offsettime);
    }
}


Output:

TIME: 03:14:11.212Z

Program 2:




// Java program to demonstrate
// OffsetTime of(
// LocalTime, ZoneOffset) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(
        String[] args)
    {
        // Create LocalTime object
        LocalTime time
            = LocalTime.of(
                8, 45, 40, 50);
  
        // Create ZoneOffset object
        ZoneOffset offset
            = ZoneOffset.ofHoursMinutes(
                5, 30);
  
        // Create OffsetTime object
        OffsetTime offsettime
            = OffsetTime.of(
                time, offset);
  
        // Print time
        System.out.println(
            "TIME: "
            + offsettime);
    }
}


Output:

TIME: 08:45:40.000000050+05:30

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments