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.
- 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.
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:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.