首頁  >  文章  >  Java  >  在Java 9中,ofInstant()方法的重要性是什麼?

在Java 9中,ofInstant()方法的重要性是什麼?

WBOY
WBOY轉載
2023-09-13 13:57:031035瀏覽

在Java 9中,ofInstant()方法的重要性是什么?

在 Java 9 中,引入了 ofInstant() 方法進行轉換。它是 LocalDateLocalTimeLocalDateTime 類別的靜態方法。此方法將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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除