
在 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>











