Java 8 – How to compare two LocalDate instances ?

In this article, we will discuss different ways to compare two instances of LocalDate in Java 1.8 version

1. Comparing two LocalDate instances :

There are different methods available to compare 2 LocalDate instances, those are –

  1. compareTo(ChronoLocalDate) –
    • This method compares invoking LocalDate with another LocalDate
  2. isBefore(ChronoLocalDate) –
    • This method checks if invoking LocalDate is before the specified LocalDate
  3. isAfter(ChronoLocalDate) –
    • This method checks if invoking LocalDate is after the specified LocalDate
  4. isEqual(ChronoLocalDate) –
    • This method checks if invoking LocalDate is equal to the specified LocalDate
  5. equals(Object) –
    • This method checks if invoking LocalDate is equal to another LocalDate

2. Examples for Comparing 2 LocalDate instances :

  1. Using compareTo(ChronoLocalDate) method
  2. Using isBefore(ChronoLocalDate) method
  3. Using isAfter(ChronoLocalDate) method
  4. Using isEqual(ChronoLocalDate) method
  5. Using equals(Object) method

2.1 Using compareTo(ChronoLocalDate) method :

  • This method compares invoking LocalDate with another LocalDate and returns integer value based on the comparison
    1. Returns Zero, if both LocalDate in comparison are equal
    2. Returns Positive value, if invoking LocalDate is latter-than (greater-than) the specified LocalDate
    3. Returns Negative value, if invoking LocalDate is earlier-than (lesser-than) the specified LocalDate

Compare2LocalDateUsingCompareToMethod.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;

public class Compare2LocalDateUsingCompareToMethod {

	public static void main(String[] args) {

		// 1. get today's LocalDate
		LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 3);
		System.out.println("1. Today's LocalDate is = " + todaysLocalDate);


		// 2. form past LocalDate
		LocalDate pastLocalDate = LocalDate.of(2022, Month.JANUARY, 1);
		System.out.println("2. Past LocalDate is = " + pastLocalDate);


		// 3. form future LocalDate
		LocalDate futureLocalDate = LocalDate.of(2022, Month.DECEMBER, 31);
		System.out.println("3. Future LocalDate is = " + futureLocalDate);


		// 4. LocalDate comparison using compareTo() method
		System.out.println("\n\n4. Comparison with compareTo() method :- \n");


		// 4.1 check whether todaysLocalDate Older-than futureLocalDate
		int compareTo1 = todaysLocalDate.compareTo(futureLocalDate);
		System.out.println("4.1 Today (" + todaysLocalDate 
				+ ") is Older-than Future (" + futureLocalDate + ") ?\t " 
				+ (compareTo1 < 0));


		// 4.2 check whether todaysLocalDate Latter-than futureLocalDate
		int compareTo2 = todaysLocalDate.compareTo(futureLocalDate);
		System.out.println("4.2 Today (" + todaysLocalDate 
				+ ") is Latter-than Future (" + futureLocalDate + ") ?\t " 
				+ (compareTo2 > 0));


		// 4.3 check whether todaysLocalDate Older-than pastLocalDate
		int compareTo3 = todaysLocalDate.compareTo(pastLocalDate);
		System.out.println("4.3 Today (" + todaysLocalDate 
				+ ") is Older than Past (" + pastLocalDate + ") ?\t " 
				+ (compareTo3 < 0));


		// 4.4 check whether todaysLocalDate Latter-than pastLocalDate
		int compareTo4 = todaysLocalDate.compareTo(pastLocalDate);
		System.out.print("4.4 Today (" + todaysLocalDate 
				+ ") is Latter-than Past (" + pastLocalDate + ") ?\t " 
				+ (compareTo4 > 0));
	}
}

Output:

1. Today's LocalDate is = 2022-08-03
2. Past LocalDate is = 2022-01-01
3. Future LocalDate is = 2022-12-31


4. Comparison with compareTo() method :- 

4.1 Today (2022-08-03) is Older-than Future (2022-12-31) ?	 true
4.2 Today (2022-08-03) is Latter-than Future (2022-12-31) ?	 false
4.3 Today (2022-08-03) is Older than Past (2022-01-01) ?	 false
4.4 Today (2022-08-03) is Latter-than Past (2022-01-01) ?	 true

2.2 Using isBefore(ChronoLocalDate) method :

CheckLocalDateIsBeforeAnotherLocalDate.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;

public class CheckLocalDateIsBeforeAnotherLocalDate {

	public static void main(String[] args) {

		// 1. get today's LocalDate
		LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 3);
		System.out.println("1. Today's LocalDate is = " + todaysLocalDate);


		// 2. form past LocalDate
		LocalDate pastLocalDate = LocalDate.of(2022, Month.AUGUST, 1);
		System.out.println("2. Past LocalDate is = " + pastLocalDate);


		// 3. form future LocalDate
		LocalDate futureLocalDate = LocalDate.of(2022, Month.AUGUST, 29);
		System.out.println("3. Future LocalDate is = " + futureLocalDate);


		// 4. isBefore() - LocalDate comparison
		System.out.println("\n\n4. Comparing 2 LocalDate using isBefore() method :- \n");


		// 4.1 check whether todaysLocalDate isBefore futureLocalDate
		boolean isBefore1 = todaysLocalDate.isBefore(futureLocalDate);
		System.out.println("4.1 Today's LocalDate (" + todaysLocalDate 
				+ ") is Before Future LocalDate (" + futureLocalDate + ") ? "
				+ isBefore1);


		// 4.2 check whether todaysLocalDate isBefore pastLocalDate
		boolean isBefore2 = todaysLocalDate.isBefore(pastLocalDate);
		System.out.println("4.2 Today's LocalDate (" + todaysLocalDate 
				+ ") is Before Past LocalDate (" + pastLocalDate + ") ? "
				+ isBefore2);


		// 4.3 check whether pastLocalDate isBefore todaysLocalDate 
		boolean isBefore3 = pastLocalDate.isBefore(todaysLocalDate);
		System.out.println("4.3 Past LocalDate (" + pastLocalDate 
				+ ") is Before Today's LocalDate (" + todaysLocalDate + ") ? "
				+ isBefore3);


		// 4.4 check whether pastLocalDate isBefore futureLocalDate 
		boolean isBefore4 = pastLocalDate.isBefore(futureLocalDate);
		System.out.println("4.4 Past LocalDate (" + pastLocalDate 
				+ ") is Before Future LocalDate (" + futureLocalDate + ") ? "
				+ isBefore4);


		// 4.5 check whether futureLocalDate isBefore todaysLocalDate 
		boolean isBefore5 = futureLocalDate.isBefore(todaysLocalDate);
		System.out.println("4.5 Future LocalDate (" + futureLocalDate 
				+ ") is Before Today's LocalDate (" + todaysLocalDate + ") ? "
				+ isBefore5);


		// 4.6 check whether futureLocalDate isBefore pastLocalDate 
		boolean isBefore6 = futureLocalDate.isBefore(todaysLocalDate);
		System.out.print("4.6 Future LocalDate (" + futureLocalDate 
				+ ") is Before Past LocalDate (" + pastLocalDate + ") ? "
				+ isBefore6);
	}
}

Output:

1. Today's LocalDate is = 2022-08-03
2. Past LocalDate is = 2022-08-01
3. Future LocalDate is = 2022-08-29


4. Comparing 2 LocalDate using isBefore() method :- 

4.1 Today's LocalDate (2022-08-03) is Before Future LocalDate (2022-08-29) ? true
4.2 Today's LocalDate (2022-08-03) is Before Past LocalDate (2022-08-01) ? false
4.3 Past LocalDate (2022-08-01) is Before Today's LocalDate (2022-08-03) ? true
4.4 Past LocalDate (2022-08-01) is Before Future LocalDate (2022-08-29) ? true
4.5 Future LocalDate (2022-08-29) is Before Today's LocalDate (2022-08-03) ? false
4.6 Future LocalDate (2022-08-29) is Before Past LocalDate (2022-08-01) ? false

2.3 Using isAfter(ChronoLocalDate) method :

CheckLocalDateIsAfterAnotherLocalDate.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;

public class CheckLocalDateIsAfterAnotherLocalDate {

	public static void main(String[] args) {

		// 1. get today's LocalDate
		LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 3);
		System.out.println("1. Today's LocalDate is = " + todaysLocalDate);


		// 2. form past LocalDate
		LocalDate pastLocalDate = LocalDate.of(2022, Month.AUGUST, 1);
		System.out.println("2. Past LocalDate is = " + pastLocalDate);


		// 3. form future LocalDate
		LocalDate futureLocalDate = LocalDate.of(2022, Month.AUGUST, 29);
		System.out.println("3. Future LocalDate is = " + futureLocalDate);


		// 4. isAfter() - LocalDate comparison
		System.out.println("\n\n4. Comparing 2 LocalDate using isAfter() method :- \n");


		// 4.1 check whether todaysLocalDate isAfter futureLocalDate
		boolean isAfter1 = todaysLocalDate.isAfter(futureLocalDate);
		System.out.println("4.1 Today's LocalDate (" + todaysLocalDate 
				+ ") is After Future LocalDate (" + futureLocalDate + ") ? "
				+ isAfter1);


		// 4.2 check whether todaysLocalDate isAfter pastLocalDate
		boolean isAfter2 = todaysLocalDate.isAfter(pastLocalDate);
		System.out.println("4.2 Today's LocalDate (" + todaysLocalDate 
				+ ") is After Past LocalDate (" + pastLocalDate + ") ? "
				+ isAfter2);


		// 4.3 check whether pastLocalDate isAfter todaysLocalDate 
		boolean isAfter3 = pastLocalDate.isAfter(todaysLocalDate);
		System.out.println("4.3 Past LocalDate (" + pastLocalDate 
				+ ") is After Today's LocalDate (" + todaysLocalDate + ") ? "
				+ isAfter3);


		// 4.4 check whether pastLocalDate isAfter futureLocalDate 
		boolean isAfter4 = pastLocalDate.isAfter(futureLocalDate);
		System.out.println("4.4 Past LocalDate (" + pastLocalDate 
				+ ") is After Future LocalDate (" + futureLocalDate + ") ? "
				+ isAfter4);


		// 4.5 check whether futureLocalDate isAfter todaysLocalDate 
		boolean isAfter5 = futureLocalDate.isAfter(todaysLocalDate);
		System.out.println("4.5 Future LocalDate (" + futureLocalDate 
				+ ") is After Today's LocalDate (" + todaysLocalDate + ") ? "
				+ isAfter5);


		// 4.6 check whether futureLocalDate isAfter pastLocalDate 
		boolean isAfter6 = futureLocalDate.isAfter(todaysLocalDate);
		System.out.println("4.6 Future LocalDate (" + futureLocalDate 
				+ ") is After Past LocalDate (" + pastLocalDate + ") ? "
				+ isAfter6);
	}
}

Output:

1. Today's LocalDate is = 2022-08-03
2. Past LocalDate is = 2022-08-01
3. Future LocalDate is = 2022-08-29


4. Comparing 2 LocalDate using isAfter() method :- 

4.1 Today's LocalDate (2022-08-03) is After Future LocalDate (2022-08-29) ? false
4.2 Today's LocalDate (2022-08-03) is After Past LocalDate (2022-08-01) ? true
4.3 Past LocalDate (2022-08-01) is After Today's LocalDate (2022-08-03) ? false
4.4 Past LocalDate (2022-08-01) is After Future LocalDate (2022-08-29) ? false
4.5 Future LocalDate (2022-08-29) is After Today's LocalDate (2022-08-03) ? true
4.6 Future LocalDate (2022-08-29) is After Past LocalDate (2022-08-01) ? true

2.4 Using isEqual(ChronoLocalDate) method :

  • This method checks if invoking LocalDate is equal to the specified LocalDate
    1. Return true, only if both invoking LocalDate & specified LocalDate are same
    2. Return false, for all other cases

CheckTwoLocalDateAreEqualUsingIsEqualMethod.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;

public class CheckTwoLocalDateAreEqualUsingIsEqualMethod {

	public static void main(String[] args) {

		// 1. get today's LocalDate
		LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 4);
		System.out.println("1. Today's LocalDate is = " + todaysLocalDate);


		// 2. get current LocalDate
		LocalDate currentLocalDate = LocalDate.now();
		System.out.println("2. Current System Date is = " + currentLocalDate);


		// 3. form past LocalDate
		LocalDate pastLocalDate = LocalDate.of(2022, Month.AUGUST, 1);
		System.out.println("3. Past LocalDate is = " + pastLocalDate);


		// 4. form future LocalDate
		LocalDate futureLocalDate = LocalDate.of(2022, Month.AUGUST, 29);
		System.out.println("4. Future LocalDate is = " + futureLocalDate);


		// 5. LocalDate comparison using isEqual() method
		System.out.println("\n\n5. Check 2 LocalDate are equal using isEqual() method :- \n");


		// 5.1 check whether todaysLocalDate & currentLocalDate are same ?
		boolean isEqual1 = todaysLocalDate.isEqual(currentLocalDate);
		System.out.println("5.1 Today's LocalDate (" + todaysLocalDate 
				+ ") & Current LocalDate (" + currentLocalDate + ") are equal ?\t "
				+ isEqual1);


		// 5.2 check whether todaysLocalDate & pastLocalDate are same ?
		boolean isEqual2 = todaysLocalDate.isEqual(pastLocalDate);
		System.out.println("5.2 Today's LocalDate (" + todaysLocalDate 
				+ ") & Past LocalDate (" + pastLocalDate + ") are equal ?\t "
				+ isEqual2);


		// 5.3 check whether todaysLocalDate & futureLocalDate are same ?
		boolean isEqual3 = todaysLocalDate.isEqual(futureLocalDate);
		System.out.print("5.3 Today's LocalDate (" + todaysLocalDate 
				+ ") & Future LocalDate (" + futureLocalDate + ") are equal ?\t "
				+ isEqual3);
	}
}

Output:

1. Today's LocalDate is = 2022-08-04
2. Current System Date is = 2022-08-04
3. Past LocalDate is = 2022-08-01
4. Future LocalDate is = 2022-08-29


5. Check 2 LocalDate are equal using isEqual() method :- 

5.1 Today's LocalDate (2022-08-04) & Current LocalDate (2022-08-04) are equal ?	 true
5.2 Today's LocalDate (2022-08-04) & Past LocalDate (2022-08-01) are equal ?	 false
5.3 Today's LocalDate (2022-08-04) & Future LocalDate (2022-08-29) are equal ?	 false

2.5 Using equals(Object) method :

  • This method checks if invoking LocalDate is equal to the another LocalDate
    1. Return true, only if both invoking LocalDate & specified LocalDate are same
    2. Return false, for all other cases

CheckTwoLocalDateAreEqualUsingEqualsMethod.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;

public class CheckTwoLocalDateAreEqualUsingEqualsMethod {

	public static void main(String[] args) {

		// 1. get today's LocalDate
		LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 4);
		System.out.println("1. Today's LocalDate is = " + todaysLocalDate);


		// 2. get current LocalDate
		LocalDate currentLocalDate = LocalDate.now();
		System.out.println("2. Current System Date is = " + currentLocalDate);


		// 3. form past LocalDate
		LocalDate pastLocalDate = LocalDate.of(2022, Month.AUGUST, 1);
		System.out.println("3. Past LocalDate is = " + pastLocalDate);


		// 4. form future LocalDate
		LocalDate futureLocalDate = LocalDate.of(2022, Month.AUGUST, 29);
		System.out.println("4. Future LocalDate is = " + futureLocalDate);


		// 5. LocalDate comparison using isEqual() method
		System.out.println("\n\n5. Check 2 LocalDate are equal using equals() method :- \n");


		// 5.1 check whether todaysLocalDate & currentLocalDate are same ?
		boolean equals1 = todaysLocalDate.equals(currentLocalDate);
		System.out.println("5.1 Today's LocalDate (" + todaysLocalDate 
				+ ") & Current LocalDate (" + currentLocalDate + ") are equal ?\t "
				+ equals1);


		// 5.2 check whether todaysLocalDate & pastLocalDate are same ?
		boolean equals2 = todaysLocalDate.equals(pastLocalDate);
		System.out.println("5.2 Today's LocalDate (" + todaysLocalDate 
				+ ") & Past LocalDate (" + pastLocalDate + ") are equal ?\t "
				+ equals2);


		// 5.3 check whether todaysLocalDate & futureLocalDate are same ?
		boolean equals3 = todaysLocalDate.equals(futureLocalDate);
		System.out.print("5.3 Today's LocalDate (" + todaysLocalDate 
				+ ") & Future LocalDate (" + futureLocalDate + ") are equal ?\t "
				+ equals3);
	}
}

Output:

1. Today's LocalDate is = 2022-08-04
2. Current System Date is = 2022-08-04
3. Past LocalDate is = 2022-08-01
4. Future LocalDate is = 2022-08-29


5. Check 2 LocalDate are equal using equals() method :- 

5.1 Today's LocalDate (2022-08-04) & Current LocalDate (2022-08-04) are equal ?	 true
5.2 Today's LocalDate (2022-08-04) & Past LocalDate (2022-08-01) are equal ?	 false
5.3 Today's LocalDate (2022-08-04) & Future LocalDate (2022-08-29) are equal ?	 false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to get Hour, Minute and Second fields from LocalTime ?
Java 8 – How to check whether a LocalDate is After another LocalDate ?