Java 中 Instant 的 isAfter() 方法


可以使用 Java 中 Instant 類的 isAfter() 方法檢查特定 Instant 物件在時間線上是否在另一個 Instant 物件之後。此方法需要一個引數,即要比較的 Instant 物件。如果 Instant 物件在另一個 Instant 物件之後,則返回 true,否則返回 false。

一個演示此功能的程式如下

示例

 線上演示

import java.time.*;
import java.time.temporal.ChronoUnit;
public class Demo {
   public static void main(String[] args) {
      Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z");
      Instant i2 = Instant.parse("2019-01-13T11:19:28.00Z");
      boolean flag = i1.isAfter(i2);
      System.out.println("Instant object i1 is: " + i1);
      System.out.println("Instant object i2 is: " + i2);
      if(flag)
         System.out.println("
Instant object i1 is after Instant object i2");       else          System.out.println("
Instant object i1 is before Instant object i2");    } }

輸出

Instant object i1 is: 2019-01-13T16:10:35Z
Instant object i2 is: 2019-01-13T11:19:28Z

Instant object i1 is after Instant object i2

現在讓我們瞭解一下上面的程式。

顯示了兩個瞬時物件 i1 和 i2。使用 isAfter() 方法檢查 Instant 物件 i1 是否在時間線上位於 Instant 物件 i2 之後。使用 if 語句顯示返回的值。演示此過程的程式碼片段如下

Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z");
Instant i2 = Instant.parse("2019-01-13T11:19:28.00Z");
boolean flag = i1.isAfter(i2);
System.out.println("Instant object i1 is: " + i1);
System.out.println("Instant object i2 is: " + i2);
if(flag)
   System.out.println("
Instant object i1 is after Instant object i2"); else    System.out.println("
Instant object i1 is before Instant object i2");

更新於: 30-Jul-2019

157 次瀏覽

開啟你的 職業

透過完成課程,獲得認證

開始學習
廣告
© . All rights reserved.