在 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中文網其他相關文章!