Java 8 – How to compare two LocalDateTime instances ?

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

1. Comparing two LocalDateTime instances :

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

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

2. Examples for Comparing 2 LocalDateTime instances :

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

2.1 Using compareTo(ChronoLocalDateTime) method :

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

Compare2LocalDateTimeUsingCompareToMethod.java

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

import java.time.LocalDateTime;
import java.time.Month;

public class Compare2LocalDateTimeUsingCompareToMethod {

	public static void main(String[] args) {

		// 1. get Current Date/time
		LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("1. Current Date/time is = " + todaysLocalDateTime);


		// 2. form Past Date/time
		LocalDateTime pastLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 1, 1, 1, 100);
		System.out.println("2. Past Date/time is = " + pastLocalDateTime);


		// 3. form Future Date/time
		LocalDateTime futureLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 23, 59, 59, 200);
		System.out.println("3. Future Date/time is = " + futureLocalDateTime);


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


		// 4.1 check whether todaysLocalDateTime Older-than futureLocalDateTime
		int compareTo1 = todaysLocalDateTime.compareTo(futureLocalDateTime);
		System.out.println("4.1 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Older-than Future Date/time (" + futureLocalDateTime + ") ? " 
				+ (compareTo1 < 0));


		// 4.2 check whether todaysLocalDateTime Latter-than futureLocalDateTime
		int compareTo2 = todaysLocalDateTime.compareTo(futureLocalDateTime);
		System.out.println("4.2 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Latter-than Future Date/time (" + futureLocalDateTime + ") ? " 
				+ (compareTo2 > 0));


		// 4.3 check whether todaysLocalDateTime Older-than pastLocalDateTime
		int compareTo3 = todaysLocalDateTime.compareTo(pastLocalDateTime);
		System.out.println("4.3 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Older than Past Date/time (" + pastLocalDateTime + ") ? " 
				+ (compareTo3 < 0));


		// 4.4 check whether todaysLocalDateTime Latter-than pastLocalDateTime
		int compareTo4 = todaysLocalDateTime.compareTo(pastLocalDateTime);
		System.out.print("4.4 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Latter-than Past Date/time (" + pastLocalDateTime + ") ? " 
				+ (compareTo4 > 0));
	}
}

Output:

1. Current Date/time is = 2022-08-11T12:30:30.000000500
2. Past Date/time is = 2022-08-11T01:01:01.000000100
3. Future Date/time is = 2022-08-11T23:59:59.000000200


4. Comparison with compareTo() method :- 

4.1 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Older-than Future Date/time (2022-08-11T23:59:59.000000200) ? true
4.2 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Latter-than Future Date/time (2022-08-11T23:59:59.000000200) ? false
4.3 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Older than Past Date/time (2022-08-11T01:01:01.000000100) ? false
4.4 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Latter-than Past Date/time (2022-08-11T01:01:01.000000100) ? true

2.2 Using isBefore(ChronoLocalDateTime) method :

CheckLocalDateTimeIsBeforeAnotherLocalDateTime.java

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

import java.time.LocalDateTime;
import java.time.Month;

public class CheckLocalDateTimeIsBeforeAnotherLocalDateTime {

	public static void main(String[] args) {

		// 1. get Current Date/time
		LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("1. Current Date/time is = " + todaysLocalDateTime);


		// 2. form Past Date/time
		LocalDateTime pastLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 1, 1, 1, 100);
		System.out.println("2. Past Date/time is = " + pastLocalDateTime);


		// 3. form Future Date/time
		LocalDateTime futureLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 23, 59, 59, 200);
		System.out.println("3. Future Date/time is = " + futureLocalDateTime);


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


		// 4.1 check whether todaysLocalDateTime isBefore futureLocalDateTime
		boolean isBefore1 = todaysLocalDateTime.isBefore(futureLocalDateTime);
		System.out.println("4.1 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Before Future Date/time (" + futureLocalDateTime + ") ? "
				+ isBefore1);


		// 4.2 check whether todaysLocalDateTime isBefore pastLocalDateTime
		boolean isBefore2 = todaysLocalDateTime.isBefore(pastLocalDateTime);
		System.out.println("4.2 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Before Past Date/time (" + pastLocalDateTime + ") ? "
				+ isBefore2);


		// 4.3 check whether pastLocalDateTime isBefore todaysLocalDateTime 
		boolean isBefore3 = pastLocalDateTime.isBefore(todaysLocalDateTime);
		System.out.println("4.3 Past Date/time (" + pastLocalDateTime 
				+ ") \n\t\t is Before Current Date/time (" + todaysLocalDateTime + ") ? "
				+ isBefore3);


		// 4.4 check whether pastLocalDateTime isBefore futureLocalDateTime 
		boolean isBefore4 = pastLocalDateTime.isBefore(futureLocalDateTime);
		System.out.println("4.4 Past Date/time (" + pastLocalDateTime 
				+ ") \n\t\t is Before Future Date/time (" + futureLocalDateTime + ") ? "
				+ isBefore4);


		// 4.5 check whether futureLocalDateTime isBefore todaysLocalDateTime 
		boolean isBefore5 = futureLocalDateTime.isBefore(todaysLocalDateTime);
		System.out.println("4.5 Future Date/time (" + futureLocalDateTime 
				+ ") \n\t\t is Before Current Date/time (" + todaysLocalDateTime + ") ? "
				+ isBefore5);


		// 4.6 check whether futureLocalDateTime isBefore pastLocalDateTime 
		boolean isBefore6 = futureLocalDateTime.isBefore(todaysLocalDateTime);
		System.out.print("4.6 Future Date/time (" + futureLocalDateTime 
				+ ") \n\t\t is Before Past Date/time (" + pastLocalDateTime + ") ? "
				+ isBefore6);
	}
}

Output:

1. Current Date/time is = 2022-08-11T12:30:30.000000500
2. Past Date/time is = 2022-08-11T01:01:01.000000100
3. Future Date/time is = 2022-08-11T23:59:59.000000200


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

4.1 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Before Future Date/time (2022-08-11T23:59:59.000000200) ? true
4.2 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Before Past Date/time (2022-08-11T01:01:01.000000100) ? false
4.3 Past Date/time (2022-08-11T01:01:01.000000100) 
		 is Before Current Date/time (2022-08-11T12:30:30.000000500) ? true
4.4 Past Date/time (2022-08-11T01:01:01.000000100) 
		 is Before Future Date/time (2022-08-11T23:59:59.000000200) ? true
4.5 Future Date/time (2022-08-11T23:59:59.000000200) 
		 is Before Current Date/time (2022-08-11T12:30:30.000000500) ? false
4.6 Future Date/time (2022-08-11T23:59:59.000000200) 
		 is Before Past Date/time (2022-08-11T01:01:01.000000100) ? false

2.3 Using isAfter(ChronoLocalDateTime) method :

CheckLocalDateTimeIsAfterAnotherLocalDateTime.java

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

import java.time.LocalDateTime;
import java.time.Month;

public class CheckLocalDateTimeIsAfterAnotherLocalDateTime {

	public static void main(String[] args) {

		// 1. get Current Date/time
		LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("1. Current Date/time is = " + todaysLocalDateTime);


		// 2. form Past Date/time
		LocalDateTime pastLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 1, 1, 1, 100);
		System.out.println("2. Past Date/time is = " + pastLocalDateTime);


		// 3. form Future Date/time
		LocalDateTime futureLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 23, 59, 59, 200);
		System.out.println("3. Future Date/time is = " + futureLocalDateTime);



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


		// 4.1 check whether todaysLocalDateTime isAfter futureLocalDateTime
		boolean isAfter1 = todaysLocalDateTime.isAfter(futureLocalDateTime);
		System.out.println("4.1 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is After Future Date/time (" + futureLocalDateTime + ") ? "
				+ isAfter1);


		// 4.2 check whether todaysLocalDateTime isAfter pastLocalDateTime
		boolean isAfter2 = todaysLocalDateTime.isAfter(pastLocalDateTime);
		System.out.println("4.2 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is After Past Date/time (" + pastLocalDateTime + ") ? "
				+ isAfter2);


		// 4.3 check whether pastLocalDateTime isAfter todaysLocalDateTime 
		boolean isAfter3 = pastLocalDateTime.isAfter(todaysLocalDateTime);
		System.out.println("4.3 Past Date/time (" + pastLocalDateTime 
				+ ") \n\t\t is After Current Date/time (" + todaysLocalDateTime + ") ? "
				+ isAfter3);


		// 4.4 check whether pastLocalDateTime isAfter futureLocalDateTime 
		boolean isAfter4 = pastLocalDateTime.isAfter(futureLocalDateTime);
		System.out.println("4.4 Past Date/time (" + pastLocalDateTime 
				+ ") \n\t\t is After Future Date/time (" + futureLocalDateTime + ") ? "
				+ isAfter4);


		// 4.5 check whether futureLocalDateTime isAfter todaysLocalDateTime 
		boolean isAfter5 = futureLocalDateTime.isAfter(todaysLocalDateTime);
		System.out.println("4.5 Future Date/time (" + futureLocalDateTime 
				+ ") \n\t\t is After Current Date/time (" + todaysLocalDateTime + ") ? "
				+ isAfter5);


		// 4.6 check whether futureLocalDateTime isAfter pastLocalDateTime 
		boolean isAfter6 = futureLocalDateTime.isAfter(todaysLocalDateTime);
		System.out.print("4.6 Future Date/time (" + futureLocalDateTime 
				+ ") \n\t\t is After Past Date/time (" + pastLocalDateTime + ") ? "
				+ isAfter6);
	}
}

Output:

1. Current Date/time is = 2022-08-11T12:30:30.000000500
2. Past Date/time is = 2022-08-11T01:01:01.000000100
3. Future Date/time is = 2022-08-11T23:59:59.000000200


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

4.1 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is After Future Date/time (2022-08-11T23:59:59.000000200) ? false
4.2 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is After Past Date/time (2022-08-11T01:01:01.000000100) ? true
4.3 Past Date/time (2022-08-11T01:01:01.000000100) 
		 is After Current Date/time (2022-08-11T12:30:30.000000500) ? false
4.4 Past Date/time (2022-08-11T01:01:01.000000100) 
		 is After Future Date/time (2022-08-11T23:59:59.000000200) ? false
4.5 Future Date/time (2022-08-11T23:59:59.000000200) 
		 is After Current Date/time (2022-08-11T12:30:30.000000500) ? true
4.6 Future Date/time (2022-08-11T23:59:59.000000200) 
		 is After Past Date/time (2022-08-11T01:01:01.000000100) ? true

2.4 Using isEqual(ChronoLocalDateTime) method :

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

CheckTwoLocalDateTimeAreEqualUsingIsEqualMethod.java

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

import java.time.LocalDateTime;
import java.time.Month;

public class CheckTwoLocalTimeDateAreEqualUsingIsEqualMethod {

	public static void main(String[] args) {

		// 1. get Current Date/time 1
		LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("1. Today's Date/time is = " + todaysLocalDateTime);


		// 2. get Current Date/time 2
		LocalDateTime currentLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("2. Current Date/time is = " + currentLocalDateTime);


		// 3. form Past Date/time
		LocalDateTime pastLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 1, 1, 1, 100);
		System.out.println("2. Past Date/time is = " + pastLocalDateTime);


		// 4. form Future Date/time
		LocalDateTime futureLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 23, 59, 59, 200);
		System.out.println("3. Future Date/time is = " + futureLocalDateTime);



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


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


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


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

Output:

1. Today's Date/time is = 2022-08-11T12:30:30.000000500
2. Current Date/time is = 2022-08-11T12:30:30.000000500
2. Past Date/time is = 2022-08-11T01:01:01.000000100
3. Future Date/time is = 2022-08-11T23:59:59.000000200


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

5.1 Today's LocalDateTime (2022-08-11T12:30:30.000000500) 
		 & Current LocalDateTime (2022-08-11T12:30:30.000000500) are equal ?	 true
5.2 Today's LocalDateTime (2022-08-11T12:30:30.000000500) 
		 & Past LocalDateTime (2022-08-11T01:01:01.000000100) are equal ?	 false
5.3 Today's LocalDateTime (2022-08-11T12:30:30.000000500) 
		 & Future LocalDateTime (2022-08-11T23:59:59.000000200) are equal ?	 false

2.5 Using equals(Object) method :

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

CheckTwoLocalDateTimeAreEqualUsingEqualsMethod.java

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

import java.time.LocalDateTime;
import java.time.Month;

public class CheckTwoLocalDateTimeAreEqualUsingEqualsMethod {

	public static void main(String[] args) {

		// 1. get Current Date/time 1
		LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("1. Today's Date/time is = " + todaysLocalDateTime);


		// 2. get Current Date/time 2
		LocalDateTime currentLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
		System.out.println("2. Current Date/time is = " + currentLocalDateTime);


		// 3. form Past Date/time
		LocalDateTime pastLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 1, 1, 1, 100);
		System.out.println("2. Past Date/time is = " + pastLocalDateTime);


		// 4. form Future Date/time
		LocalDateTime futureLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 23, 59, 59, 200);
		System.out.println("3. Future Date/time is = " + futureLocalDateTime);



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


		// 5.1 check whether todaysLocalDateTime & todaysLocalDateTime are same ?
		boolean isEqual1 = todaysLocalDateTime.equals(todaysLocalDateTime);
		System.out.println("5.1 Today's LocalDateTime (" + todaysLocalDateTime 
				+ ") \n\t\t & Current LocalDateTime (" + todaysLocalDateTime + ") are equal ?\t "
				+ isEqual1);


		// 5.2 check whether todaysLocalDateTime & pastLocalDateTime are same ?
		boolean isEqual2 = todaysLocalDateTime.equals(pastLocalDateTime);
		System.out.println("5.2 Today's LocalDateTime (" + todaysLocalDateTime 
				+ ") \n\t\t & Past LocalDateTime (" + pastLocalDateTime + ") are equal ?\t "
				+ isEqual2);


		// 5.3 check whether todaysLocalDateTime & futureLocalDateTime are same ?
		boolean isEqual3 = todaysLocalDateTime.equals(futureLocalDateTime);
		System.out.print("5.3 Today's LocalDateTime (" + todaysLocalDateTime 
				+ ") \n\t\t & Future LocalDateTime (" + futureLocalDateTime + ") are equal ?\t "
				+ isEqual3);
	}
}

Output:

1. Today's Date/time is = 2022-08-11T12:30:30.000000500
2. Current Date/time is = 2022-08-11T12:30:30.000000500
2. Past Date/time is = 2022-08-11T01:01:01.000000100
3. Future Date/time is = 2022-08-11T23:59:59.000000200


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

5.1 Today's LocalDateTime (2022-08-11T12:30:30.000000500) 
		 & Current LocalDateTime (2022-08-11T12:30:30.000000500) are equal ?	 true
5.2 Today's LocalDateTime (2022-08-11T12:30:30.000000500) 
		 & Past LocalDateTime (2022-08-11T01:01:01.000000100) are equal ?	 false
5.3 Today's LocalDateTime (2022-08-11T12:30:30.000000500) 
		 & Future LocalDateTime (2022-08-11T23:59:59.000000200) are equal ?	 false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to find difference between two LocalDateTime using Period/Duration ?
Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?