Compare time in JavaMost of the time we need to compare two date and date-time objects. Date comparison is required when we want to get the data of some specific date and time from the database or to filter the returned data based on date and time. In order to compare time, we use the compareTo() method of the LocalTime class. The compareTo() method of the class compares two LocalTime objects. The first LocalTime object is one which we want to compare, and the second object is which we pass to the compareTo() method as a parameter and from which we want to compare this LocalTime object. Syntax:The compareTo() method of the LocalTime class is as follows: Parameters:It accepts only a single parameter, i.e., the LocalTime object, which is going to be compared, and it should not be null. Returns:The compareTo() method returns three values based on the comparison of the objects:
Let's take some examples to understand how the compareTo() method is used for the comparison of two LocalTime objects. CompareToExample1.java Output: Let's take one more example to understand the concept of the compareTo() method. In this example, we will compare two date-time objects by using the compareTo() method. CompareToExample2.java Output: Next TopicCompare Two Sets in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India