In Java 9, the ofInstant() method has introduced for conversion. It is a static method of LocalDate, LocalTime, and LocalDateTime classes. This method converts java.time.Instant object to LocalDate that requires a time zone in the form of java.time.ZoneId.
Syntax
public static LocalTime ofInstant(Instant instant, ZoneId zone) public static LocalDate ofInstant(Instant instant, ZoneId zone) public static LocalDateTime ofInstant(Instant instant, ZoneId zone)
Example
import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.Instant; import java.time.ZoneId; public class OfInstantMethodTest { public static void main(String args[]) { Instant instant = Instant.parse("2020-02-05T02:35:15.245Z"); System.out.println("Instant: " + instant); LocalDate ld = LocalDate.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalDate: " + ld); LocalTime lt = LocalTime.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalTime: " + lt); LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Kolkata")); System.out.println("LocalDateTime: " + ldt); } }
Output
Instant: 2020-02-05T02:35:15.245Z LocalDate: 2020-02-05 LocalTime: 08:05:15.245 LocalDateTime: 2020-02-05T08:05:15.245