Skip to content
Related Articles

Related Articles

Instant isBefore() method in Java with Examples
  • Last Updated : 27 Nov, 2018

isBefore() method of an Instant class checks if this instant timeline position is before the instant passed as parameter or not. If this instant timeline position is before the instant passed as a parameter then the method will return true else false. The comparison is based on the time-line position of the instants.

Syntax:

public boolean isBefore(Instant otherInstant)

Parameter: This method takes a parameter otherInstant which is the other instant to compare to. It should not be null.

Returns: This method returns true if this instant is before the specified instant.

Exception: This method throws NullPointerException if otherInstant is null.



Below programs illustrate the isBefore() method:

Program 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);
    }
}
Output:
Instance 1: 2018-12-30T09:24:54.630Z
Instance 2: 2018-12-31T01:34:00.630Z
Is Instant1 before Instant2: true

Program 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);
    }
}
Output:
Instance 1: 2018-11-27T09:24:54.630Z
Instance 2: 2018-11-27T04:55:36.127Z
Is Instant1 before Instant2: false

Program 3: To show Exception thrown by 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);
        }
    }
}
Output:
Instance 1: 2018-10-30T19:34:50.630Z
Instance 2: null
Exception: java.lang.NullPointerException

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :