java after方法_Java Instant isAfter()用法及代码示例

Instant类的isAfter()方法用于检查此瞬间是否在作为参数传递的瞬间之后。此方法返回一个显示相同值的布尔值。

用法:

public boolean isAfter(Instant otherInstant)

参数:此方法采用参数otherInstant,该参数是要与该瞬间进行比较的另一瞬间。它不能为空。

返回值:如果此时刻在指定时刻之后,则此方法返回true。否则返回false。

异常:如果以参数为null传递的otherInstant,则此方法将引发NullPointerException。

以下示例程序旨在说明isAfter()方法:

示例1:

// Java program to demonstrate

// Instant.isAfter() method

import java.time.*;

public class GFG {

public static void main(String[] args)

{

// create a Instant object

Instant instant1

= Instant.parse("2018-12-30T19:34:50.63Z");

// create other Instant

Instant instant2

= Instant.parse("2018-12-29T09:24:00.63Z");

// print instances

System.out.println("Instance 1: " + instant1);

System.out.println("Instance 2: " + instant2);

// check if instant1 is after instant2

// using isAfter()

boolean value = instant1.isAfter(instant2);

// print result

System.out.println("Is Instant1 after Instant2: "

+ value);

}

}

输出:

Instance 1: 2018-12-30T19:34:50.630Z

Instance 2: 2018-12-29T09:24:00.630Z

Is Instant1 after Instant2: true

示例2:

// Java program to demonstrate

// Instant.isAfter() method

import java.time.*;

public class GFG {

public static void main(String[] args)

{

// create a Instant object

Instant instant1

= Instant.parse("2018-10-30T19:34:50.63Z");

// create other Instant

Instant instant2 = Instant.now();

// print instances

System.out.println("Instance 1: " + instant1);

System.out.println("Instance 2: " + instant2);

// check if instant1 is after instant2

// using isAfter()

boolean value = instant1.isAfter(instant2);

// print result

System.out.println("Is Instant1 after Instant2: "

+ value);

}

}

输出:

Instance 1: 2018-10-30T19:34:50.630Z

Instance 2: 2018-11-27T04:52:08.970Z

Is Instant1 after Instant2: false

示例3:显示isAfter()引发的异常

// Java program to demonstrate

// Instant.isAfter() method

import java.time.*;

public class GFG {

public static void main(String[] args)

{

// create a Instant object

Instant instant1

= Instant.parse("2018-10-30T19:34:50.63Z");

// create other Instant

Instant instant2 = null;

try {

// print instances

System.out.println("Instance 1: " + instant1);

System.out.println("Instance 2: " + instant2);

// check if instant1 is after instant2

// using isAfter()

boolean value = instant1.isAfter(instant2);

}

catch (Exception e) {

// print result

System.out.println("Exception: " + e);

}

}

}

输出:

Instance 1: 2018-10-30T19:34:50.630Z

Instance 2: null

Exception: java.lang.NullPointerException

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值