Saturday, October 18, 2025
HomeLanguagesJavaOffsetTime ofInstant() method in Java with examples

OffsetTime ofInstant() method in Java with examples

The ofInstant() method of a OffsetTime class is used to obtain an instance of OffsetTime from an Instant and zone ID passed as parameters.In this method First, the offset from UTC/Greenwich is obtained using the zone ID and instant. Then, the local time has calculated from the instant and offset.

Syntax:

public static OffsetTime 
       ofInstant(Instant instant, ZoneId zone)

Parameters: This method accepts two parameters:

  • instant: It is the instant from which the OffsetTime object is to be created. It should not be null.
  • zone: It is the zone of the specified time. It should not be null.

Return value: This method returns the created OffsetTime object created from the passed instant.

Below program illustrate the ofInstant() method:




// Java program to demonstrate
// OffsetTime.ofInstant() method
  
import java.time.OffsetTime;
import java.time.Instant;
import java.time.ZoneId;
  
public class GFG {
    public static void main(String[] args)
    {
        // Creates an instance
        OffsetTime time = OffsetTime.ofInstant(Instant.now(), 
                                       ZoneId.systemDefault());
  
        System.out.println("Offset time: " + time);
    }
}


Output:

Offset time: 03:17:43.019Z

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#ofInstant-java.time.Instant-java.time.ZoneId-

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS