Friday, September 5, 2025
HomeLanguagesJavaInstant isAfter() method in Java with Examples

Instant isAfter() method in Java with Examples

isAfter() method of an Instant class is used to check if this instant is after the instant passed as parameter or not. This method returns a boolean value showing the same.

Syntax:

public boolean isAfter(Instant otherInstant)

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

Returns: This method returns true if this instant is after the specified instant. Else it returns false.

Exception: This method throws NullPointerException if otherInstant passed as the parameter is null.

Below programs illustrate the isAfter() method:

Program 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);
    }
}


Output:

Instance 1: 2018-12-30T19:34:50.630Z
Instance 2: 2018-12-29T09:24:00.630Z
Is Instant1 after Instant2: true

Program 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);
    }
}


Output:

Instance 1: 2018-10-30T19:34:50.630Z
Instance 2: 2018-11-27T04:52:08.970Z
Is Instant1 after Instant2: false

Program 3: To show Exception thrown by 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);
        }
    }
}


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#isAfter(java.time.Instant)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11864 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS