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

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

Compare 2 LocalTime using isAfter() method :

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

CheckLocalTimeIsAfterAnotherLocalTime.java

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

import java.time.LocalTime;

public class CheckLocalTimeIsAfterAnotherLocalTime {

	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. isAfter() - LocalTime comparison
		System.out.println("\n\n4. Comparing 2 LocalTime using isAfter() method :- \n");


		// 4.1 check whether currentTime isAfter lastTime
		boolean isAfter1 = currentTime.isAfter(lastTime);
		System.out.println("4.1 Current LocalTime (" + currentTime 
				+ ") is After Future LocalTime (" + lastTime + ") ? "
				+ isAfter1);


		// 4.2 check whether currentTime isAfter pastTime
		boolean isAfter2 = currentTime.isAfter(pastTime);
		System.out.println("4.2 Current LocalTime (" + currentTime 
				+ ") is After Past LocalTime (" + pastTime + ") ? "
				+ isAfter2);


		// 4.3 check whether pastTime isAfter currentTime 
		boolean isAfter3 = pastTime.isAfter(currentTime);
		System.out.println("4.3 Past LocalTime (" + pastTime 
				+ ") is After Current LocalTime (" + currentTime + ") ? "
				+ isAfter3);


		// 4.4 check whether pastTime isAfter lastTime 
		boolean isAfter4 = pastTime.isAfter(lastTime);
		System.out.println("4.4 Past LocalTime (" + pastTime 
				+ ") is After Future LocalTime (" + lastTime + ") ? "
				+ isAfter4);


		// 4.5 check whether lastTime isAfter currentTime 
		boolean isAfter5 = lastTime.isAfter(currentTime);
		System.out.println("4.5 Future LocalTime (" + lastTime 
				+ ") is After Current LocalTime (" + currentTime + ") ? "
				+ isAfter5);


		// 4.6 check whether lastTime isAfter pastTime 
		boolean isAfter6 = lastTime.isAfter(currentTime);
		System.out.println("4.6 Future LocalTime (" + lastTime 
				+ ") is After Past LocalTime (" + pastTime + ") ? "
				+ isAfter6);



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


		// 5.1 check whether currentTime isAfter currentTime
		boolean isAfter7 = currentTime.isAfter(currentTime);
		System.out.println("5.1 Current LocalTime (" + currentTime 
				+ ") is After Current LocalTime (" + currentTime + ") ? "
				+ isAfter7);


		// 5.2 check whether pastTime isAfter pastTime
		boolean isAfter8 = pastTime.isAfter(pastTime);
		System.out.println("5.2 Past LocalTime (" + pastTime 
				+ ") is After Past LocalTime (" + pastTime + ") ? "
				+ isAfter8);


		// 5.3 check whether pastTime isAfter pastTime
		boolean isAfter9 = pastTime.isAfter(pastTime);
		System.out.print("5.3 Future LocalTime (" + pastTime 
				+ ") is After Future LocalTime (" + pastTime + ") ? "
				+ isAfter9);
	}
}

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 isAfter() method :- 

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


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

5.1 Current LocalTime (17:29:59.000000999) is After Current LocalTime (17:29:59.000000999) ? false
5.2 Past LocalTime (00:00) is After Past LocalTime (00:00) ? false
5.3 Future LocalTime (00:00) is After Future LocalTime (00:00) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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