Java Instant isBefore()方法及实例

Java Instant isBefore()方法及实例

isBefore() 方法是即时类的方法,它检查这个即时的时间线位置是否在作为参数传递的即时之前。如果这个瞬间的时间线位置在作为参数传递的瞬间之前,那么这个方法将返回true,否则返回false。这种比较是基于瞬间的时间线位置的。

语法

public boolean isBefore(Instant otherInstant)

参数: 该方法需要一个参数 otherInstant ,它是要与之比较的另一个瞬间。它不应该是空的。

返回: 如果这个时刻在指定的时刻之前,该方法返回 true

异常: 如果otherInstant为空,该方法会抛出 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)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程