Java Compare two LocalDate Values
Tags: Java LocalDate
In this Java core tutorial we learn how to compare two java.time.LocalDate objects in Java programming language using compareTo(), isEqual(), equals(), isBefore() and isAfter() methods.
Table of contents
- Compare two LocalDate objects using compareTo() method
- Compare two LocalDate objects for Equality
- Compare if a LocalDate before another LocalDate
- Compare if a LocalDate after another LocalDate
Compare two LocalDate objects using compareTo() method
In Java, to compare two LocalDate objects for ordering we can use the LocalDate.compareTo() method which return int value of -1, 0 or 1 if the LocalDate object is before, equal to or after the LocalDate object it compare to.
The following Java example code to show the LocalDate.compareTo() method return -1 when the LocalDate object is before another LocalDate object it compare to.
CompareLocalDateExample1.java
import java.time.LocalDate;
public class CompareLocalDateExample1 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 10);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
int result = localDate1.compareTo(localDate2);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate1 compareTo localDate2: " + result);
}
}
localDate1: 2022-07-10
localDate2: 2022-07-11
localDate1 compareTo localDate2: -1
The following Java example code to show the LocalDate.compareTo() method return 0 when the LocalDate object is equal to another LocalDate object it compare to.
CompareLocalDateExample2.java
import java.time.LocalDate;
public class CompareLocalDateExample2 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
int result = localDate1.compareTo(localDate2);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate1 compareTo localDate2: " + result);
}
}
localDate1: 2022-07-11
localDate2: 2022-07-11
localDate1 compareTo localDate2: 0
The following Java example code to show the LocalDate.compareTo() method return 1 when the LocalDate object is after another LocalDate object it compare to.
CompareLocalDateExample3.java
import java.time.LocalDate;
public class CompareLocalDateExample3 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 12);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
int result = localDate1.compareTo(localDate2);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate1 compareTo localDate2: " + result);
}
}
localDate1: 2022-07-12
localDate2: 2022-07-11
localDate1 compareTo localDate2: 1
Compare two LocalDate objects for Equality
In Java to compare two LocalDate values are equal or not we can use the LocalDate.isEqual() method as the following example Java code.
CompareLocalDateExample4.java
import java.time.LocalDate;
public class CompareLocalDateExample4 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
boolean result1 = localDate1.isEqual(localDate2);
boolean result2 = localDate1.isEqual(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 is equal to localDate2: " + result1);
System.out.println("localDate1 is equal to localDate3: " + result2);
}
}
localDate1: 2022-07-11
localDate2: 2022-07-11
localDate3: 2022-07-12
localDate1 is equal to localDate2: true
localDate1 is equal to localDate3: false
The LocalDate class also provide equals() method which return the same result with isEqual() method, we learn how to use the LocalDate.equals() method in the following Java program.
CompareLocalDateExample5.java
import java.time.LocalDate;
public class CompareLocalDateExample5 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 11);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 12);
boolean result1 = localDate1.equals(localDate2);
boolean result2 = localDate1.equals(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 is equal to localDate2: " + result1);
System.out.println("localDate1 is equal to localDate3: " + result2);
}
}
localDate1: 2022-07-11
localDate2: 2022-07-11
localDate3: 2022-07-12
localDate1 is equal to localDate2: true
localDate1 is equal to localDate3: false
Compare if a LocalDate before another LocalDate
In Java to check if a LocalDate object is before another specified LocalDate object we can use the LocalDate.isBefore() method as following Java code.
CompareLocalDateExample6.java
import java.time.LocalDate;
public class CompareLocalDateExample6 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 10);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 9);
boolean result1 = localDate1.isBefore(localDate2);
boolean result2 = localDate1.isBefore(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 is before localDate2: " + result1);
System.out.println("localDate1 is before localDate3: " + result2);
}
}
localDate1: 2022-07-10
localDate2: 2022-07-11
localDate3: 2022-07-09
localDate1 is before localDate2: true
localDate1 is before localDate3: false
Compare if a LocalDate after another LocalDate
In Java to check if a LocalDate object is after another specified LocalDate object we can use the LocalDate.isAfter() method as following Java code.
CompareLocalDateExample7.java
import java.time.LocalDate;
public class CompareLocalDateExample7 {
public static void main(String... args) {
LocalDate localDate1 = LocalDate.of(2022,7, 10);
LocalDate localDate2 = LocalDate.of(2022,7, 11);
LocalDate localDate3 = LocalDate.of(2022,7, 9);
boolean result1 = localDate1.isAfter(localDate2);
boolean result2 = localDate1.isAfter(localDate3);
System.out.println("localDate1: " + localDate1);
System.out.println("localDate2: " + localDate2);
System.out.println("localDate3: " + localDate3);
System.out.println("localDate1 is after localDate2: " + result1);
System.out.println("localDate1 is after localDate3: " + result2);
}
}
localDate1: 2022-07-10
localDate2: 2022-07-11
localDate3: 2022-07-09
localDate1 is after localDate2: false
localDate1 is after localDate3: true
Happy Coding 😊
Related Articles
Java Compare two Calendar Values
Java Compare two LocalTime Values