In Java 9, the ofInstant() method was introduced for conversion. It is a static method of the LocalDate, LocalTime and LocalDateTime classes. This method converts the java.time.Instant object to LocalDate, requiring a time zone in the form of java.time.ZoneId.
<strong>public static LocalTime ofInstant(Instant instant, ZoneId zone) public static LocalDate ofInstant(Instant instant, ZoneId zone) public static LocalDateTime ofInstant(Instant instant, ZoneId zone)</strong>
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[]) { <strong>Instant </strong>instant = Instant.<strong>parse</strong>("2020-02-05T02:35:15.245Z"); System.out.println("Instant: " + instant); <strong>LocalDate </strong>ld = LocalDate.<strong>ofInstant</strong>(instant, <strong>ZoneId</strong>.of("Asia/Kolkata")); System.out.println("LocalDate: " + ld); <strong>LocalTime </strong>lt = LocalTime.<strong>ofInstant</strong>(instant, <strong>ZoneId</strong>.of("Asia/Kolkata")); System.out.println("LocalTime: " + lt); <strong>LocalDateTime </strong>ldt = LocalDateTime.<strong>ofInstant</strong>(instant, <strong>ZoneId</strong>.of("Asia/Kolkata")); System.out.println("LocalDateTime: " + ldt); } }
<strong>Instant: 2020-02-05T02:35:15.245Z LocalDate: 2020-02-05 LocalTime: 08:05:15.245 LocalDateTime: 2020-02-05T08:05:15.245</strong>
The above is the detailed content of What is the importance of ofInstant() method in Java 9?. For more information, please follow other related articles on the PHP Chinese website!