Java program to compare time using compareTo() method
// Java program to compare time using
// compareTo() method
import java.util.*;
import java.time.*;
public class Main {
public static void main(String[] args) {
LocalTime time1;
LocalTime time2;
LocalTime time3;
time1 = LocalTime.of(10, 15, 18); //10:15:18
time2 = LocalTime.of(11, 45, 46); //11:45:46
time3 = LocalTime.of(11, 45, 46); //11:45:46
if (time1.compareTo(time2) > 0)
System.out.println("time1 is later than time2.");
else if (time1.compareTo(time2) < 0)
System.out.println("time2 is later than time1.");
else
System.out.println("time1 and time2 are same.");
if (time2.compareTo(time3) > 0)
System.out.println("time2 is later than time3.");
else if (time2.compareTo(time3) < 0)
System.out.println("time3 is later than time2.");
else
System.out.println("time2 and time3 are same.");
}
}
Output
time2 is later than time1.
time2 and time3 are same.