Java 8 – How to compare two Instant instances ?

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

1. Comparing two Instant instances :

There are different methods available to compare two Instant instances, those are –

  1. compareTo(Instant) –
    • This method compares invoking Instant with another Instant
  2. isBefore(Instant) –
    • This method checks if invoking Instant is before the specified Instant
  3. isAfter(Instant) –
    • This method checks if invoking Instant is after the specified Instant

2. Examples for Comparing two Instant :

  1. Using compareTo(Instant) method
  2. Using isBefore(Instant) method
  3. Using isAfter(Instant) method

2.1 Using compareTo(Instant) method :

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

CompareTwoInstantUsingCompareToMethod.java

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

import java.time.Instant;

public class CompareTwoInstantUsingCompareToMethod {

	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. Instant comparison using compareTo() method
		System.out.println("\n\n4. Comparison with compareTo() method :- \n");


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


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


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


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

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. Comparison with compareTo() method :- 

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

2.2 Using isBefore(Instant) method :

  • This method checks if invoking Instant is before the specified Instant
    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 Instant objects
  • Read Java 8 – How to check whether an Instant is Before another Instant ? for more details and examples

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);
	}
}

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

2.3 Using isAfter(Instant) method :

  • This method checks if invoking Instant is after the specified Instant
    1. Returns true, if invoking Instant is after the specified Instant
    2. Returns false, if invoking Instant is not after the specified Instant
    3. Always returns false for self-comparing Instant objects
  • Read Java 8 – How to check whether an Instant is After another Instant ? for more details and examples

CheckInstantIsAfterAnotherInstant.java

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

import java.time.Instant;

public class CheckInstantIsAfterAnotherInstant {

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


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


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


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


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


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


		// 4.6 check whether instantFuture isAfter instantPast 
		boolean isAfter6 = instantFuture.isAfter(instantNow);
		System.out.println("4.6 Future Instant (" + instantFuture 
				+ ") \n\t\t is After Past Instant (" + instantPast + ") ? "
				+ isAfter6);
	}
}

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

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

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to find difference between two Instant instances using Duration ?
Java 8 – How to check whether an Instant is After another Instant ?