Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

LocalTime compareTo() method in Java with Examples

  • Last Updated : 03 Dec, 2018

The compareTo() method of a LocalTime class is used to compare this LocalTime object to the LocalTime passed as parameter to check whether both LocalTimes are equal. The comparison between both LocalTimes is based on timeline position of the local times within a day. The value to be returned by this method is determined as follows:

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

Syntax:

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.

public int compareTo(LocalTime other)

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

Return Value: This method returns the int value and returned value is determined as follows:



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

Below programs illustrate the compareTo() method:

Program 1: When this LocalTime is greater




// Java program to demonstrate
// LocalTime.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Objects
        LocalTime time1
            = LocalTime.parse("17:52:49");
        LocalTime time2
            = LocalTime.parse("13:08:00");
  
        // apply compareTo()
        int value = time1.compareTo(time2);
  
        // print LocalDateTime
        System.out.println("Int Value:" + value);
  
        if (value > 0)
            System.out.println("LocalTime1 is greater");
        else if (value == 0)
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2");
        else
            System.out.println("LocalTime2 is greater");
    }
}
Output:
Int Value:1
LocalTime1 is greater

Program 2: When passed LocalTime is greater




// Java program to demonstrate
// LocalTime.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Objects
        LocalTime time1
            = LocalTime.parse("07:12:29");
        LocalTime time2
            = LocalTime.parse("13:08:00");
  
        // apply compareTo()
        int value = time1.compareTo(time2);
  
        // print LocalDateTime
        System.out.println("Int Value:" + value);
  
        if (value > 0)
            System.out.println("LocalTime1 is greater");
        else if (value == 0)
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2");
        else
            System.out.println("LocalTime2 is greater");
    }
}
Output:
Int Value:-1
LocalTime2 is greater

Program 3: When both Localtimes are equal




// Java program to demonstrate
// LocalTime.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Objects
        LocalTime time1
            = LocalTime.parse("13:08:00");
        LocalTime time2
            = LocalTime.parse("13:08:00");
  
        // apply compareTo()
        int value = time1.compareTo(time2);
  
        // print LocalDateTime
        System.out.println("Int Value:" + value);
  
        if (value > 0)
            System.out.println("LocalTime1 is greater");
        else if (value == 0)
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2");
        else
            System.out.println("LocalTime2 is greater");
    }
}
Output:
Int Value:0
LocalTime1 is equal to LocalTime2

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#compareTo(java.time.LocalTime))




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!