Javatime-duration-between

提供:Dev Guides
移動先:案内検索

java.time.Duration.between()メソッドの例

説明

  • java.time.Duration.between()*メソッドは、2つの一時オブジェクト間の期間を表すDurationを取得します。

宣言

以下は* java.time.Duration.between()*メソッドの宣言です。

public static Duration between(Temporal startInclusive, Temporal endExclusive)

パラメーター

  • startInclusive -開始インスタント、包括的、null以外。
  • endExclusive -終了インスタント、排他的、null以外。

戻り値

nullではないDuration

例外

  • DateTimeException -テンポラル間の秒を取得できない場合
  • ArithmeticException -計算がDurationの容量を超える場合。

次の例は、java.time.Duration.between()メソッドの使用法を示しています。

package com.finddevguides;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;

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

      Duration duration = Duration.between(LocalTime.NOON,LocalTime.MAX);

      LocalDateTime date = LocalDateTime.now();
      System.out.println(date);

      date = (LocalDateTime)duration.addTo(date);
      System.out.println(date);
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

2017-03-07T15:45:39.456
2017-03-08T03:45:39.455999999