当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Instant isBefore()用法及代码示例


Instant类的isBefore()方法检查此即时时间轴位置是否在作为参数传递的即时数据之前。如果此瞬时时间轴位置在作为参数传递的瞬时之前,则该方法将返回true或false。比较是基于瞬时的时间线位置。

用法:

public boolean isBefore(Instant otherInstant)

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


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

异常:如果otherInstant为null,则此方法引发NullPointerException。

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

示例1:

// Java program to demonstrate 
// Instant.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant1 
            = Instant.parse("2018-12-30T09:24:54.63Z"); 
  
        // create other Instant 
        Instant instant2 
            = Instant.parse("2018-12-31T01:34: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.isBefore(instant2); 
  
        // print result 
        System.out.println("Is Instant1 before Instant2: "
                           + value); 
    } 
}
输出:
Instance 1: 2018-12-30T09:24:54.630Z
Instance 2: 2018-12-31T01:34:00.630Z
Is Instant1 before Instant2: true

示例2:

// Java program to demonstrate 
// Instant.isBefore() method 
  
import java.time.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a Instant object 
        Instant instant1 
            = Instant.parse("2018-11-27T09:24:54.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.isBefore(instant2); 
  
        // print result 
        System.out.println("Is Instant1 before Instant2: "
                           + value); 
    } 
}
输出:
Instance 1: 2018-11-27T09:24:54.630Z
Instance 2: 2018-11-27T04:55:36.127Z
Is Instant1 before Instant2: false

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

// Java program to demonstrate 
// Instant.isBefore() 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.isBefore(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

参考:https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#isBefore(java.time.Instant)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Instant isBefore() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。