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



說明

java.time.ZonedDateTime.until(Temporal endExclusive, TemporalUnit unit) 方法計算到另一個日期時間的持續時間(以指定單位表示)。

宣告

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

public long until(Temporal endExclusive, TemporalUnit unit)

```java

  • 引數

  • endDateExclusive - 結束日期,不包括在內,已轉換為 ZonedDateTime,不為 null。

unit - 用來度量數量的單位,不為 null。

返回值

此日期時間和結束日期時間之間的持續時間。

  • 異常

  • DateTimeException - 如果無法計算數量,或者無法將結束時間轉換為 ZonedDateTime。

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

ArithmeticException - 如果發生數字溢位。

示例

package com.tutorialspoint;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class ZonedDateTimeDemo {
   public static void main(String[] args) {
      
      ZonedDateTime date = ZonedDateTime.parse("2017-03-28T12:25:38.492+05:30[Asia/Calcutta]");
      ZonedDateTime date1 = ZonedDateTime.now();
      System.out.println(date.until(date1, ChronoUnit.HOURS));  
   }
}

現場演示

6603
列印頁面
© . All rights reserved.