Java 8 – How to check whether an Instant is Before another Instant ?

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

Compare two Instant using isBefore() method :

  • If there are two Instant and the requirement is to check whether an Instant is before another Instant, then isBefore() method can be used
    • isBefore(Instant) – checks if the invoking Instant is before the specified Instant
  • Comparison results –
    1. Returns true, if invoking Instant is before the specified Instant
    2. Returns false, if invoking Instant is not before the specified Instant
    3. Always returns false for self comparing Instants
  • Lets see an example for comparing two Instant using isBefore() method

CheckInstantIsBeforeAnotherInstant.java

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

import java.time.Instant;

public class CheckInstantIsBeforeAnotherInstant {

	public static void main(String[] args) {

		// 1. get current Instant at UTC
		String instantNowInStr = "2022-08-21T07:15:37.308075900Z";
		Instant instantNow = Instant.parse(instantNowInStr);
		System.out.println("1. Current Instant is \t\t = " + instantNow);


		// 2. parse future Instant 
		String instantFutureInStr = "2022-12-25T15:10:23.933345200Z";
		Instant instantFuture = Instant.parse(instantFutureInStr);
		System.out.println("2. Parsed FUTURE Instant is \t = " + instantFuture);


		// 3. parse past Instant 
		String instantPastInStr = "2022-01-26T15:10:23.933345200Z";
		Instant instantPast = Instant.parse(instantPastInStr);
		System.out.println("3. Parsed PAST Instant is \t = " + instantPast);



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


		// 4.1 check whether instantNow isBefore instantFuture
		boolean isBefore1 = instantNow.isBefore(instantFuture);
		System.out.println("4.1 Current Instant (" + instantNow 
				+ ") \n\t\t is Before Future Instant (" + instantFuture + ") ? "
				+ isBefore1);


		// 4.2 check whether instantNow isBefore instantPast
		boolean isBefore2 = instantNow.isBefore(instantPast);
		System.out.println("4.2 Current Instant (" + instantNow 
				+ ") \n\t\t is Before Past Instant (" + instantPast + ") ? "
				+ isBefore2);


		// 4.3 check whether instantPast isBefore instantNow 
		boolean isBefore3 = instantPast.isBefore(instantNow);
		System.out.println("4.3 Past Instant (" + instantPast 
				+ ") \n\t\t is Before Current Instant (" + instantNow + ") ? "
				+ isBefore3);


		// 4.4 check whether instantPast isBefore instantFuture 
		boolean isBefore4 = instantPast.isBefore(instantFuture);
		System.out.println("4.4 Past Instant (" + instantPast 
				+ ") \n\t\t is Before Future Instant (" + instantFuture + ") ? "
				+ isBefore4);


		// 4.5 check whether instantFuture isBefore instantNow 
		boolean isBefore5 = instantFuture.isBefore(instantNow);
		System.out.println("4.5 Future Instant (" + instantFuture 
				+ ") \n\t\t is Before Current Instant (" + instantNow + ") ? "
				+ isBefore5);


		// 4.6 check whether instantFuture isBefore instantPast 
		boolean isBefore6 = instantFuture.isBefore(instantNow);
		System.out.println("4.6 Future Instant (" + instantFuture 
				+ ") \n\t\t is Before Past Instant (" + instantPast + ") ? "
				+ isBefore6);



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


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


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


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

Output:

1. Current Instant is 		 = 2022-08-21T07:15:37.308075900Z
2. Parsed FUTURE Instant is 	 = 2022-12-25T15:10:23.933345200Z
3. Parsed PAST Instant is 	 = 2022-01-26T15:10:23.933345200Z


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

4.1 Current Instant (2022-08-21T07:15:37.308075900Z) 
		 is Before Future Instant (2022-12-25T15:10:23.933345200Z) ? true
4.2 Current Instant (2022-08-21T07:15:37.308075900Z) 
		 is Before Past Instant (2022-01-26T15:10:23.933345200Z) ? false
4.3 Past Instant (2022-01-26T15:10:23.933345200Z) 
		 is Before Current Instant (2022-08-21T07:15:37.308075900Z) ? true
4.4 Past Instant (2022-01-26T15:10:23.933345200Z) 
		 is Before Future Instant (2022-12-25T15:10:23.933345200Z) ? true
4.5 Future Instant (2022-12-25T15:10:23.933345200Z) 
		 is Before Current Instant (2022-08-21T07:15:37.308075900Z) ? false
4.6 Future Instant (2022-12-25T15:10:23.933345200Z) 
		 is Before Past Instant (2022-01-26T15:10:23.933345200Z) ? false


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

5.1 Current Instant (2022-08-21T07:15:37.308075900Z) 
		 is Before Current Instant (2022-08-21T07:15:37.308075900Z) ? false
5.2 Past Instant (2022-01-26T15:10:23.933345200Z) 
		 is Before Past Instant (2022-01-26T15:10:23.933345200Z) ? false
5.3 Future Instant (2022-12-25T15:10:23.933345200Z) 
		 is Before Future Instant (2022-12-25T15:10:23.933345200Z) ? false

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether an Instant is After another Instant ?
Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?