Skip to content
Related Articles

Related Articles

Instant compareTo() method in Java with Examples
  • Last Updated : 30 Jul, 2019

The compareTo(Instant otherInstant) method of Instant class used to compare this instance to the instance passed as parameter to check whether both instances are equal. The comparison between both instances is based on the time-line position of the instants. The value to be returned by this method is determined as follows:

  • if this instance is greater than instance passed as a parameter, then a positive value is returned
  • if this instance is equal to the instance passed as a parameter, then a zero (0) is returned
  • if this instance is less than instance passed as a parameter then a negative value is returned.

Syntax:

public int compareTo(Instant otherInstant)

Parameters: This method accepts a mandatory parameter otherInstant which is the other instant to be compared, and it should not be null.

Return Value: This method returns an integer value as follows:

  • if this instance is greater than instance passed as a parameter, then a positive value is returned
  • if this instance is equal to the instance passed as a parameter, then a zero (0) is returned
  • if this instance is less than instance passed as a parameter then a negative value is returned.

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



Below programs illustrate the Instant.compareTo() method:

Program 1: When this instance is greater




// Java program to demonstrate
// Instant.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create two instance objects
        Instant instant1
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        Instant instant2
            = Instant.parse("2017-10-20T16:55:30.00Z");
  
        // print Instant Values
        System.out.println("Instant1: "
                           + instant1);
        System.out.println("Instant2: "
                           + instant2);
  
        // compare both instant
        int value = instant1.compareTo(instant2);
  
        if (value > 0)
            System.out.println("Instant1 is greater");
        else if (value == 0)
            System.out.println("Instant1 is equal to Instant2");
        else
            System.out.println("Instant2 is greater");
    }
}


Output:

Instant1: 2018-10-20T16:55:30Z
Instant2: 2017-10-20T16:55:30Z
Instant1 is greater

Program 2: When the otherInstant passed as parameter is greater




// Java program to demonstrate
// Instant.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create two instance objects
        Instant instant1
            = Instant.parse("2017-10-20T16:55:30.00Z");
  
        Instant instant2
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        // print Instant Values
        System.out.println("Instant1: "
                           + instant1);
        System.out.println("Instant2: "
                           + instant2);
  
        // compare both instant
        int value = instant1.compareTo(instant2);
  
        if (value > 0)
            System.out.println("Instant1 is greater");
        else if (value == 0)
            System.out.println("Instant1 is equal to Instant2");
        else
            System.out.println("Instant2 is greater");
    }
}


Output:

Instant1: 2017-10-20T16:55:30Z
Instant2: 2018-10-20T16:55:30Z
Instant2 is greater

Program 1: When both instances are equal




// Java program to demonstrate
// Instant.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create two instance objects
        Instant instant1
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        Instant instant2
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        // print Instant Values
        System.out.println("Instant1: "
                           + instant1);
        System.out.println("Instant2: "
                           + instant2);
  
        // compare both instant
        int value = instant1.compareTo(instant2);
  
        if (value > 0)
            System.out.println("Instant1 is greater");
        else if (value == 0)
            System.out.println("Instant1 is equal to Instant2");
        else
            System.out.println("Instant2 is greater");
    }
}


Output:

Instant1: 2018-10-20T16:55:30Z
Instant2: 2018-10-20T16:55:30Z
Instant1 is equal to Instant2

Program 4: Showing NullPointer Exception




// Java program to demonstrate
// Instant.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create two instance objects
        Instant instant1
            = Instant.parse("2018-10-20T16:55:30.00Z");
  
        Instant instant2 = null;
  
        try {
  
            // compare both instant
            int value = instant1.compareTo(instant2);
        }
        catch (NullPointerException e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#compareTo(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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :