LocalDateTime ofInstant() method in Java with Examples

ofInstant(Instant instant, ZoneId zone) method of LocalDateTime class in Java is used to create an instance of LocalDateTime using an Instant and zone ID. These two parameters are passed to the method and method returns LocalDateTime on the basis of these two parameters. The calculation of the LocalDateTime follows the following step.

  1. The zone Id and instant are used to obtain the offset from UTC/Greenwich as there can be only one valid offset for each instance.
  2. Finally, the local date-time is calculated using the instant and the obtained offset.

Syntax:

public static LocalDateTime 
       ofInstant(Instant instant,
                 ZoneId zone)

Parameters: The method accepts two parameters:

  • instant – It is of Instant type and represents the instant passed to create localdatetime.
  • zone – It is of ZoneId type and represents the time-zone used for creating the offset.

Return Value: This method returns the localdate-time.

Exceptions: This method throws DateTimeException if the result exceeds the supported range.



Below programs illustrate the ofInstant(Instant instant, ZoneId zone) method in Java:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Date: 2020-05-13T12:40:38.087

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// LocalDateTime.ofInstant(
// Instant instant, ZoneId zone) method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // Create LocalDateTime object
        LocalDateTime localdatetime
            = LocalDateTime.ofInstant(
                Instant.now(),
                ZoneId.systemDefault());
  
        // Print year only
        System.out.println(
            "Year: " + localdatetime.getYear());
    }
}

chevron_right


Output:

Year: 2020

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.