Javatime-localdatetime-until

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

java.time.LocalDateTime.until()メソッドの例

説明

  • java.time.LocalDateTime.until(Temporal endExclusive、TemporalUnit unit)*メソッドは、指定された単位に関して、別の日時までの時間を計算します。

宣言

以下は、* java.time.LocalDateTime.until(Temporal endExclusive、TemporalUnit unit)*メソッドの宣言です。

public long until(Temporal endExclusive, TemporalUnit unit)

パラメーター

  • endDateExclusive -LocalDateTimeに変換される排他的な終了日、nullではない。
  • unit -金額を測定する単位、null以外。

戻り値

この日時と終了日時の間の時間。

例外

  • DateTimeException -金額を計算できない場合、または終了時間をLocalDateTimeに変換できない場合。
  • UnsupportedTemporalTypeException -ユニットがサポートされていない場合。
  • ArithmeticException -数値オーバーフローが発生した場合。

次の例は、java.time.LocalDateTime.until(Temporal endExclusive、TemporalUnit unit)メソッドの使用方法を示しています。

package com.finddevguides;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

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

      LocalDateTime date = LocalDateTime.parse("2017-01-03T10:15:30");
      LocalDateTime date1 = LocalDateTime.now();
      System.out.println(date.until(date1, ChronoUnit.HOURS));
   }
}

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

1758