Java Compare two LocalTime Values

Tags: Java LocalTime Java 8

In this Java core tutorial we learn how to compare two java.time.LocalTime objects in Java programming language using compareTo(), equals(), isBefore() and isAfter() methods.

Table of contents

  1. Compare two LocalTime objects using compareTo() method
  2. Compare two LocalTime objects for Equality
  3. Compare if a LocalTime object is before another LocalTime object
  4. Compare if a LocalTime object is after another LocalTime object

Compare two LocalTime objects using compareTo() method

In Java, to compare two LocalTime objects for ordering we can use the LocalTime.compareTo() method which return int value of -1, 0 or 1 if the LocalTime object is before, equal to or after the LocalTime object it compare to.

The following Java example code to show the LocalTime.compareTo() method return -1 when the LocalTime object is before another LocalTime object it compare to.

CompareLocalTimeExample1.java

import java.time.LocalTime;

public class CompareLocalTimeExample1 {
    public static void main(String... args) {
        LocalTime localTime1 = LocalTime.of(7, 20, 5);
        LocalTime localTime2 = LocalTime.of(7, 20, 10);

        int compareToResult = localTime1.compareTo(localTime2);

        System.out.println("localTime1: " + localTime1);
        System.out.println("localTime2: " + localTime2);
        System.out.println("localTime1 compareTo localTime2: " + compareToResult);

    }
}
The output as below.
localTime1: 07:20:05
localTime2: 07:20:10
localTime1 compareTo localTime2: -1

The following Java example code to show the LocalTime.compareTo() method return 0 when the LocalTime object is equal to another LocalTime object it compare to.

CompareLocalTimeExample2.java

import java.time.LocalTime;

public class CompareLocalTimeExample2 {
    public static void main(String... args) {
        LocalTime localTime1 = LocalTime.of(7, 20, 10);
        LocalTime localTime2 = LocalTime.of(7, 20, 10);

        int compareToResult = localTime1.compareTo(localTime2);

        System.out.println("localTime1: " + localTime1);
        System.out.println("localTime2: " + localTime2);
        System.out.println("localTime1 compareTo localTime2: " + compareToResult);

    }
}
The output as below.
localTime1: 07:20:10
localTime2: 07:20:10
localTime1 compareTo localTime2: 0

The following Java example code to show the LocalTime.compareTo() method return 1 when the LocalTime object is after another LocalTime object it compare to.

CompareLocalTimeExample3.java

import java.time.LocalTime;

public class CompareLocalTimeExample3 {
    public static void main(String... args) {
        LocalTime localTime1 = LocalTime.of(7, 20, 15);
        LocalTime localTime2 = LocalTime.of(7, 20, 10);

        int compareToResult = localTime1.compareTo(localTime2);

        System.out.println("localTime1: " + localTime1);
        System.out.println("localTime2: " + localTime2);
        System.out.println("localTime1 compareTo localTime2: " + compareToResult);

    }
}
The output as below.
localTime1: 07:20:15
localTime2: 07:20:10
localTime1 compareTo localTime2: 1

Compare two LocalTime objects for Equality

In Java to compare two LocalTime values are equal or not we can use the LocalTime.equals() method as the following example Java code.

CompareLocalTimeExample4.java

import java.time.LocalTime;

public class CompareLocalTimeExample4 {
    public static void main(String... args) {
        LocalTime localTime1 = LocalTime.of(7, 20, 10);
        LocalTime localTime2 = LocalTime.of(7, 20, 10);
        LocalTime localTime3 = LocalTime.of(7, 20, 15);

        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);
    }
}
The output as below.
localTime1: 07:20:10
localTime2: 07:20:10
localTime3: 07:20:15
localTime1 is equal to localTime2: true
localTime1 is equal to localTime3: false

Compare if a LocalTime object is before another LocalTime object

In Java to check if a LocalTime object is before another specified LocalTime object we can use the LocalTime.isBefore() method as following Java code.

CompareLocalTimeExample5.java

import java.time.LocalTime;

public class CompareLocalTimeExample5 {
    public static void main(String... args) {
        LocalTime localTime1 = LocalTime.of(2, 20, 20);
        LocalTime localTime2 = LocalTime.of(3, 30, 30);
        LocalTime localTime3 = LocalTime.of(1, 10, 10);

        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);
    }
}
The output as below.
localTime1: 02:20:20
localTime2: 03:30:30
localTime3: 01:10:10
localTime1 is before localTime2: true
localTime1 is before localTime3: false

Compare if a LocalTime object is after another LocalTime object

In Java to check if a LocalTime object is after another specified LocalTime object we can use the LocalTime.isAfter() method as following Java code.

CompareLocalTimeExample6.java

import java.time.LocalTime;

public class CompareLocalTimeExample6 {
    public static void main(String... args) {
        LocalTime localTime1 = LocalTime.of(2, 20, 20);
        LocalTime localTime2 = LocalTime.of(3, 30, 30);
        LocalTime localTime3 = LocalTime.of(1, 10, 10);

        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);
    }
}
The output as below.
localTime1: 02:20:20
localTime2: 03:30:30
localTime3: 01:10:10
localTime1 is after localTime2: false
localTime1 is after localTime3: true

Happy Coding 😊

Java Compare two LocalDateTime Values

Java Compare two LocalDate Values

Java Compare two Calendar Values

Java Compare two Date Values

Java Convert LocalDateTime to String

Java Convert LocalDateTime to LocalDate

Java Convert LocalDateTime to LocalTime

Java Convert LocalDateTime to OffsetDateTime

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDateTime to Calendar

Java Convert LocalDateTime to Date

Java Convert LocalDateTime to Instant

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples