java.time.Instant.from() 方法示例



說明

java.time.Instant.from(TemporalAccessor temporal) 方法由時間物件獲取 Instant 例項。

宣告

以下是 java.time.Instant.from(TemporalAccessor temporal) 方法的宣告。

public static Instant from(TemporalAccessor temporal)

引數

temporal - 要轉換的時間物件,不可為 null。

返回值

時間戳,不可為 null。

異常

DateTimeException - 如果無法轉換為 Instant。

示例

以下示例展示了 java.time.Instant.from(TemporalAccessor temporal) 方法的使用。

package com.tutorialspoint;

import java.time.Instant;
import java.time.ZonedDateTime;

public class InstantDemo {
   public static void main(String[] args) {

      ZonedDateTime zonedDateTime = ZonedDateTime.now();

      Instant instant = Instant.from(zonedDateTime);

      System.out.println(instant);
   }
}

讓我們編譯並執行以上程式,這將產生以下結果 -

2017-03-10T09:29:03.044Z
廣告
© . All rights reserved.