Compare Two Times in Java
Definition
The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects.
Here we have two objects that have to be compared: the first is a LocalTime object that we want to compare, and the second is an object that we supply as an input to the compareTo() method.
Why do we compare two times in java?
It is a usual feature to compare two date and time objects. When requesting data from a database at a given time and date, or when filtering the data that has been returned based on time and date, date comparison is necessary.
Syntax
The syntax for comparing two times in java by using the LocalTime class's compareTo() method is as follows:
public int compareTo(LocalTime other)
parameters
It takes only one parameter, a LocalTime object that is to be compared, and it must not be null
Returns
- When this LocalTime object exceeds the LocalTime object that was supplied, it returns a positive result.
- It produces a negative result when this LocalTime object is smaller than the specified LocalTime object.
- This LocalTime object returns 0 if it matches the specified LocalTime object.
Examples for comparing two times in java
Example program for comparing time in java
import java.time.*;
import java.util.Scanner;
public class Demo {
public static void main(String[] args)
{
int hour1, hour2, min1, min2, sec1, sec2;
LocalTime time1, time2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter time for initializing the first LocaleTime object:");
System.out.println("Enter hours:");
hour1 = sc.nextInt();
System.out.println("Enter minutes:");
min1 = sc.nextInt();
System.out.println("Enter seconds:");
sec1 = sc.nextInt();
time1 = LocalTime.of(hour1, min1, sec1);
System.out.println("Enter time for initializing the second LocaleTime object:");
System.out.println("Enter hours:");
hour2 = sc.nextInt();
System.out.println("Enter minutes:");
min2 = sc.nextInt();
System.out.println("Enter seconds:");
sec2 = sc.nextInt();
sc.close();
time2 = LocalTime.of(hour2, min2, sec2);
int returnVal = time1.compareTo(time2);
System.out.println("Value returned by comparTo() method:" + returnVal);
if (returnVal> 0) {
System.out.println("time1 is greater than time2.");
}else if (returnVal == 0) {
System.out.println("time1 is equal to time2.");
}else {
System.out.println("time1 is smaller than time2.");
}
}
}
Output:
C:\javap>javac Demo.java
C:\javap>java Demo
Enter time for initializing the first LocaleTime object:
Enter hours:
4
Enter minutes:
20
Enter seconds:
10
Enter time for initializing the second LocaleTime object:
Enter hours:
4
Enter minutes:
23
Enter seconds:
25
Value returned by compareTo() method:-1
time1 is smaller than time2.
We can compare the two local times by using the following methods
- The two local time objects are compared using the compareTo method
- The two local time objects are compared using the equality method
- Compared if one local time object is before another local time object
- Compared if one local time object is after another local time object
They can be explained as
1.The two local time objects are compared using the compareTo method
import java.io.*;
import java.time.LocalTime;
public class CompareLocalTime
{
public static void main(String args[])
{
LocalTime localTime1 = LocalTime.of(6, 30, 4);
LocalTime localTime2 = LocalTime.of(6, 30, 20);
int compareToResult = localTime1.compareTo(localTime2);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime1 compareTo localTime2: " + compareToResult);
}
}
Output:
C:\javap>javac CompareLocalTime.java
C:\javap>java CompareLocalTime
localTime1: 06:30:04
localTime2: 06:30:20
localTime1 compareTo localTime2: -1
2.The two local time objects are compared using the equality method
import java.io.*;
import java.time.LocalTime;
public class CompareLocalTime
{
public static void main(String args[])
{
LocalTime localTime1 = LocalTime.of(7, 30, 11);
LocalTime localTime2 = LocalTime.of(7, 30, 12);
LocalTime localTime3 = LocalTime.of(7, 30, 17);
boolean equalsResult1 = localTime1.equals(localTime2);
boolean equalsResult2 = localTime1.equals(localTime3);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 is equal to localTime2: " + equalsResult1);
System.out.println("localTime1 is equal to localTime3: " + equalsResult2);
}
}
Output:
C:\javap>javac CompareLocalTime.java
C:\javap>java CompareLocalTime
localTime1: 07:30:11
localTime2: 07:30:12
localTime3: 07:30:17
localTime1 is equal to localTime2: false
localTime1 is equal to localTime3: false
3.Compared if one local time object is before another local time object
import java.io.*;
import java.time.LocalTime;
public class CompareLocalTime
{
public static void main(String args[])
{
LocalTime localTime1 = LocalTime.of(1, 10, 30);
LocalTime localTime2 = LocalTime.of(2, 20, 40);
LocalTime localTime3 = LocalTime.of(3, 30, 40);
boolean equalsResult1 = localTime1.isBefore(localTime2);
boolean equalsResult2 = localTime1.isBefore(localTime3);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 is before localTime2: " + equalsResult1);
System.out.println("localTime1 is before localTime3: " + equalsResult2);
}
}
Output:
C:\javap>javac CompareLocalTime.java
C:\javap>java CompareLocalTime
localTime1: 01:10:30
localTime2: 02:20:40
localTime3: 03:30:40
localTime1 is before localTime2: true
localTime1 is before localTime3: true
4.Compared if one local time object is after another local time object
import java.io.*;
import java.time.LocalTime;
public class CompareLocalTime
{
public static void main(String args[])
{
LocalTime localTime1 = LocalTime.of(3, 20, 10);
LocalTime localTime2 = LocalTime.of(2, 30, 20);
LocalTime localTime3 = LocalTime.of(4, 10, 30);
boolean equalsResult1 = localTime1.isAfter(localTime2);
boolean equalsResult2 = localTime1.isAfter(localTime3);
System.out.println("localTime1: " + localTime1);
System.out.println("localTime2: " + localTime2);
System.out.println("localTime3: " + localTime3);
System.out.println("localTime1 is after localTime2: " + equalsResult1);
System.out.println("localTime1 is after localTime3: " + equalsResult2);
}
}
Output:
C:\javap>javac CompareLocalTime.java
C:\javap>java CompareLocalTime
localTime1: 03:20:10
localTime2: 02:30:20
localTime3: 04:10:30
localTime1 is after localTime2: true
localTime1 is after localTime3: false