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");
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP