Javatime-instant-isbefore

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

java.time.Instant.isBefore()メソッドの例

説明

  • java.time.Instant.isBefore(Instant otherInstant)*メソッドは、このインスタントが指定されたインスタントの前かどうかをチェックします。

宣言

以下は、* java.time.Instant.isBefore(Instant otherInstant)*メソッドの宣言です。

public boolean isBefore(Instant otherInstant)

パラメーター

*otherInstant* -比較する他のインスタント、nullではない。

戻り値

このインスタントが指定されたインスタントの前にある場合はtrue。

例外

*NullPointerException* -otherInstantがnullの場合。

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

package com.finddevguides;

import java.time.Instant;

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

      Instant instant = Instant.parse("2017-02-03T10:37:30.00Z");
      System.out.println("Instant #1: " + instant);

      Instant instant1 = Instant.parse("2017-03-03T10:37:30.00Z");
      System.out.println("Instant #2: " + instant1);

      boolean result = instant.isBefore(instant1);
      System.out.println(result ? "Instant #1 is before Instant #2."
         :"Instant #1 is not before as Instant #2.");
   }
}

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

Instant #1: 2017-02-03T10:37:30Z
Instant #2: 2017-03-03T10:37:30Z
Instant #1 is before Instant #2.