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



描述

java.time.Instant.until(Temporal endExclusive, TemporalUnit unit) 方法根據指定單位計算直至另一個時間的持續時間。

宣告

以下是對 java.time.Instant.until(Temporal endExclusive, TemporalUnit unit) 方法的宣告。

public long until(Temporal endExclusive, TemporalUnit unit)

引數

  • endExclusive − 結束日期(獨佔),轉換為一個非 null Instant。

  • unit − 用於測量量的單位,非 null。

返回值

本時間和結束時間之間的持續時間。

異常

  • DateTimeException − 如果無法計算數量,或不能將結束 Temporal 轉換為 Instant。

  • UnsupportedTemporalTypeException − 如果不支援該單位。

  • ArithmeticException − 如果發生數字溢位。

示例

以下示例展示了 java.time.Instant.until(Temporal endExclusive, TemporalUnit unit) 方法的用法。

package com.tutorialspoint;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

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

      Instant instant = Instant.parse("2017-12-03T10:15:30.00Z");
      Instant instant1 = Instant.parse("2017-12-03T10:18:30.00Z");
      System.out.println(instant.until(instant1, ChronoUnit.SECONDS));
   }
}

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

180
廣告
© . All rights reserved.