In this article, we will learn how to check whether an Instant is After another Instant using Instant.isAfter() method provided in Java 1.8 version
Compare two Instant using isAfter() method :
- If there are two Instant and the requirement is to check whether an Instant is after another Instant, then isAfter() method can be used
- isAfter(Instant) – checks if the invoking Instant is after the specified Instant
- Comparison results –
- Returns true, if invoking Instant is after the specified Instant
- Returns false, if invoking Instant is not after the specified Instant
- Always returns false for self comparing Instants
- Lets see an example for comparing two Instant using isAfter() method
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);
// 5. isAfter() - self Instant comparison
System.out.println("\n\n5. Self comparing Instant using isAfter() method :- \n");
// 5.1 check whether todaysLocalDate isAfter todaysLocalDate
boolean isAfter7 = instantNow.isAfter(instantNow);
System.out.println("5.1 Current Instant (" + instantNow
+ ") \n\t\t is After Current Instant (" + instantNow + ") ? "
+ isAfter7);
// 5.2 check whether pastLocalDate isAfter pastLocalDate
boolean isAfter8 = instantPast.isAfter(instantPast);
System.out.println("5.2 Past Instant (" + instantPast
+ ") \n\t\t is After Past Instant (" + instantPast + ") ? "
+ isAfter8);
// 5.3 check whether futureLocalDate isAfter futureLocalDate
boolean isAfter9 = instantFuture.isAfter(instantFuture);
System.out.print("5.3 Future Instant (" + instantFuture
+ ") \n\t\t is After Future Instant (" + instantFuture + ") ? "
+ isAfter9);
}
}
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
5. Self comparing Instant using isAfter() method :-
5.1 Current Instant (2022-08-21T07:15:37.308075900Z)
is After Current Instant (2022-08-21T07:15:37.308075900Z) ? false
5.2 Past Instant (2022-01-26T15:10:23.933345200Z)
is After Past Instant (2022-01-26T15:10:23.933345200Z) ? false
5.3 Future Instant (2022-12-25T15:10:23.933345200Z)
is After Future Instant (2022-12-25T15:10:23.933345200Z) ? false
Related Articles:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!