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

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

Compare 2 LocalTime using isBefore() method :

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

CheckLocalTimeIsBeforeAnotherLocalTime.java

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

import java.time.LocalTime;

public class CheckLocalTimeIsBeforeAnotherLocalTime {

	public static void main(String[] args) {

		// 1. get current system time
		LocalTime currentTime = LocalTime.of(17, 29, 59, 999);
		System.out.println("1. Current System Time = " + currentTime);


		// 2. form past LocalTime
		LocalTime pastTime = LocalTime.MIDNIGHT;
		System.out.println("2. Past LocalTime is = " + pastTime);


		// 3. form future LocalTime
		LocalTime lastTime = LocalTime.MAX;
		System.out.println("3. Future LocalTime is = " + lastTime);


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


		// 4.1 check whether currentTime isBefore lastTime
		boolean isBefore1 = currentTime.isBefore(lastTime);
		System.out.println("4.1 Current LocalTime (" + currentTime 
				+ ") is Before Future LocalTime (" + lastTime + ") ? "
				+ isBefore1);


		// 4.2 check whether currentTime isBefore pastTime
		boolean isBefore2 = currentTime.isBefore(pastTime);
		System.out.println("4.2 Current LocalTime (" + currentTime 
				+ ") is Before Past LocalTime (" + pastTime + ") ? "
				+ isBefore2);


		// 4.3 check whether pastTime isBefore currentTime 
		boolean isBefore3 = pastTime.isBefore(currentTime);
		System.out.println("4.3 Past LocalTime (" + pastTime 
				+ ") is Before Current LocalTime (" + currentTime + ") ? "
				+ isBefore3);


		// 4.4 check whether pastTime isBefore lastTime 
		boolean isBefore4 = pastTime.isBefore(lastTime);
		System.out.println("4.4 Past LocalTime (" + pastTime 
				+ ") is Before Future LocalTime (" + lastTime + ") ? "
				+ isBefore4);


		// 4.5 check whether lastTime isBefore currentTime 
		boolean isBefore5 = lastTime.isBefore(currentTime);
		System.out.println("4.5 Future LocalTime (" + lastTime 
				+ ") is Before Current LocalTime (" + currentTime + ") ? "
				+ isBefore5);


		// 4.6 check whether lastTime isBefore pastTime 
		boolean isBefore6 = lastTime.isBefore(pastTime);
		System.out.println("4.6 Future LocalTime (" + lastTime 
				+ ") is Before Past LocalTime (" + pastTime + ") ? "
				+ isBefore6);



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


		// 5.1 check whether currentTime isBefore currentTime
		boolean isBefore7 = currentTime.isBefore(currentTime);
		System.out.println("5.1 Current LocalTime (" + currentTime 
				+ ") is Before current LocalTime (" + currentTime + ") ? "
				+ isBefore7);


		// 5.2 check whether pastTime isBefore pastTime
		boolean isBefore8 = pastTime.isBefore(pastTime);
		System.out.println("5.2 Past LocalTime (" + pastTime 
				+ ") is Before Past LocalTime (" + pastTime + ") ? "
				+ isBefore8);


		// 5.3 check whether lastTime isBefore lastTime
		boolean isBefore9 = lastTime.isBefore(lastTime);
		System.out.print("5.3 Future LocalTime (" + lastTime 
				+ ") is Before Future LocalTime (" + lastTime + ") ? "
				+ isBefore9);
	}
}

Output:

1. Current System Time = 17:29:59.000000999
2. Past LocalTime is = 00:00
3. Future LocalTime is = 23:59:59.999999999


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

4.1 Current LocalTime (17:29:59.000000999) is Before Future LocalTime (23:59:59.999999999) ? true
4.2 Current LocalTime (17:29:59.000000999) is Before Past LocalTime (00:00) ? false
4.3 Past LocalTime (00:00) is Before Current LocalTime (17:29:59.000000999) ? true
4.4 Past LocalTime (00:00) is Before Future LocalTime (23:59:59.999999999) ? true
4.5 Future LocalTime (23:59:59.999999999) is Before Current LocalTime (17:29:59.000000999) ? false
4.6 Future LocalTime (23:59:59.999999999) is Before Past LocalTime (00:00) ? false


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

5.1 Current LocalTime (17:29:59.000000999) is Before current LocalTime (17:29:59.000000999) ? false
5.2 Past LocalTime (00:00) is Before Past LocalTime (00:00) ? false
5.3 Future LocalTime (23:59:59.999999999) is Before Future LocalTime (23:59:59.999999999) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a LocalTime is After another LocalTime ?
Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?