Java中的即时isAfter()方法

使用isAfter()Java中Instant类中的方法,可以检查特定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("\nInstant object i1 is after Instant object i2");
      else
         System.out.println("\nInstant 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。使用方法检查即时对象i1在时间轴中是否在即时对象i2之后isAfter()。使用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("\nInstant object i1 is after Instant object i2");
else
   System.out.println("\nInstant object i1 is before Instant object i2");