在 Java 9 中,引入了 ofInstant() 方法进行转换。它是 LocalDate、LocalTime 和 LocalDateTime 类的静态方法。该方法将java.time.Instant对象转换为LocalDate,需要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>
以上是在Java 9中,ofInstant()方法的重要性是什么?的详细内容。更多信息请关注PHP中文网其他相关文章!