Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?

In this article, we will learn how to check whether one LocalDateTime is Before another LocalDateTime using LocalDateTime.isBefore() method in Java 1.8 version

Compare 2 LocalDateTime using isBefore() method :

  • If there are 2 LocalDateTime and the requirement is to check whether one LocalDateTime is before another LocalDateTime, then isBefore() method can be used
    • isBefore(ChronoLocalDateTime) – checks if the invoking LocalDateTime is before the specified LocalDateTime
  • Lets see an example for comparing 2 LocalDateTime using isBefore() method
  • Note: Self comparing LocalDateTime using isBefore() method always returns false, as shown in the below illustration

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.println("4.6 Future Date/time (" + futureLocalDateTime 
				+ ") \n\t\t is Before Past Date/time (" + pastLocalDateTime + ") ? "
				+ isBefore6);



		// 5. isBefore() - self LocalDate comparison
		System.out.println("\n\n5. Self comparing LocalDateTime using isBefore() method :- \n");


		// 5.1 check whether todaysLocalDate isBefore todaysLocalDate
		boolean isBefore7 = todaysLocalDateTime.isBefore(todaysLocalDateTime);
		System.out.println("5.1 Current Date/time (" + todaysLocalDateTime 
				+ ") \n\t\t is Before Current Date/time (" + todaysLocalDateTime + ") ? "
				+ isBefore7);


		// 5.2 check whether pastLocalDate isBefore pastLocalDate
		boolean isBefore8 = pastLocalDateTime.isBefore(pastLocalDateTime);
		System.out.println("5.2 Past Date/time (" + pastLocalDateTime 
				+ ") \n\t\t is Before Past Date/time (" + pastLocalDateTime + ") ? "
				+ isBefore8);


		// 5.3 check whether futureLocalDate isBefore futureLocalDate
		boolean isBefore9 = futureLocalDateTime.isBefore(futureLocalDateTime);
		System.out.print("5.3 Future Date/time (" + futureLocalDateTime 
				+ ") \n\t\t is Before Future Date/time (" + futureLocalDateTime + ") ? "
				+ isBefore9);
	}
}

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


5. Self comparing LocalDateTime using isBefore() method :- 

5.1 Current Date/time (2022-08-11T12:30:30.000000500) 
		 is Before Current Date/time (2022-08-11T12:30:30.000000500) ? false
5.2 Past Date/time (2022-08-11T01:01:01.000000100) 
		 is Before Past Date/time (2022-08-11T01:01:01.000000100) ? false
5.3 Future Date/time (2022-08-11T23:59:59.000000200) 
		 is Before Future Date/time (2022-08-11T23:59:59.000000200) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
Java 8 – How to alter Date and Time fields of LocalDateTime ?