OffsetDateTime ofInstant() method in Java with Examples

The ofInstant(Instant instant, ZoneId zone) method of the OffsetDateTime class in Java is used to create an instance of OffsetDateTime from the specified instant and zoneID. Since there is only one valid offset for each instant in UTC/Greenwich, it is simple to derive an offset from the latter.

Syntax:

public static OffsetDateTime
       ofInstant(Instant instant,
                 ZoneId zone)

Parameters: This method accepts two parameters:

  • instant – It is of Instant type and represents the instant at which offsetdatetime is created. It should not be null.
  • zone – It is of ZoneId type and represents the zone for the time. It should not be null.

Return value: This method returns the OffsetDateTime created from the specified parameters.

Exception: This method throws DateTimeException if the result is beyond the supported range.



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

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// OffsetDateTime ofInstant() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create OffsetDateTime object
        OffsetDateTime offsetdatetime
            = OffsetDateTime.ofInstant(
                Instant.now(),
                ZoneId.systemDefault());
  
        // Print date-time
        System.out.println("DATE-TIME: "
                           + offsetdatetime);
    }
}

chevron_right


Output:

DATE-TIME: 2020-05-20T04:05:38.471Z

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// OffsetDateTime ofInstant() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create Instant object
        Instant instant = Instant.now(
            Clock.systemUTC());
  
        // Create ZoneId object
        ZoneId zone = ZoneId.of("Z");
  
        // Create OffsetDateTime object
        OffsetDateTime offsetdatetime
            = OffsetDateTime.ofInstant(
                instant,
                zone);
  
        // Print date-time
        System.out.println("DATE-TIME: "
                           + offsetdatetime);
    }
}

chevron_right


Output:

DATE-TIME: 2020-05-20T04:05:42.166Z

References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetDateTime.html#ofInstant(java.time.Instant, java.time.ZoneId)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.