Monday, November 24, 2025
HomeLanguagesJavaLocalDate ofInstant() method in Java with Examples

LocalDate ofInstant() method in Java with Examples

The ofInstant(Instant instant, ZoneId zone) method of LocalDate class in Java is used to create an instance of LocalDate from an Instant and zone ID. These two parameters are passed to the method and on the basis of those, an instance of LocalDate is created. The calculation of the LocalDate 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 is calculated using the instant and the obtained offset.

Syntax:

public static LocalDate 
       ofInstant(Instant instant,
                 ZoneId zone)

Parameters: This method accepts two parameters:

  • instant: It is of Instant type and represents the instant passed to create the date.
  • zone: It is of ZoneId type and represent the offset.

Return Value: This method returns the localdate.

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

Note: This method is included in LocalDate class in the latest version of Java only, therefore it might not run in few of the online compilers.

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




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


Output:

Date: 2020-05-13

Program 2:




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


Output:

Year: 2020

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

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

Most Popular

Dominic
32412 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6787 POSTS0 COMMENTS
Nicole Veronica
11934 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6910 POSTS0 COMMENTS
Ted Musemwa
7169 POSTS0 COMMENTS
Thapelo Manthata
6868 POSTS0 COMMENTS
Umr Jansen
6855 POSTS0 COMMENTS