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.
Java
Java
- 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.
- Finally, the local date is calculated using the instant and the obtained offset.
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.
// 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:
Program 2:
Date: 2020-05-13
// 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:
References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#ofInstant(java.time.Instant, java.time.ZoneId)Year: 2020