In this article, we will learn how to check whether one LocalDateTime is After another LocalDateTime using LocalDateTime.isAfter() method in Java 1.8 version
Compare 2 LocalDateTime using isAfter() method :
- If there are 2 LocalDateTime and the requirement is to check whether one LocalDateTime is after another LocalDateTime, then isAfter() method can be used
- isAfter(ChronoLocalDateTime) – checks if the invoking LocalDateTime is after the specified LocalDateTime
- Lets see an example for comparing 2 LocalDateTime using isAfter() method
- Note: Self comparing LocalDateTime using isAfter() method always returns false, as shown in the below illustration
CheckLocalDateTimeIsAfterAnotherLocalDateTime.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.Month;
public class CheckLocalDateTimeIsAfterAnotherLocalDateTime {
public static void main(String[] args) {
// 1. get Current Date/time
LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
System.out.println("1. Current Date/time is = " + todaysLocalDateTime);
// 2. form Past Date/time
LocalDateTime pastLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 1, 1, 1, 100);
System.out.println("2. Past Date/time is = " + pastLocalDateTime);
// 3. form Future Date/time
LocalDateTime futureLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 23, 59, 59, 200);
System.out.println("3. Future Date/time is = " + futureLocalDateTime);
// 4. isAfter() - LocalDateTime comparison
System.out.println("\n\n4. Comparing 2 LocalDateTime using isAfter() method :- \n");
// 4.1 check whether todaysLocalDateTime isAfter futureLocalDateTime
boolean isAfter1 = todaysLocalDateTime.isAfter(futureLocalDateTime);
System.out.println("4.1 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is After Future Date/time (" + futureLocalDateTime + ") ? "
+ isAfter1);
// 4.2 check whether todaysLocalDateTime isAfter pastLocalDateTime
boolean isAfter2 = todaysLocalDateTime.isAfter(pastLocalDateTime);
System.out.println("4.2 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is After Past Date/time (" + pastLocalDateTime + ") ? "
+ isAfter2);
// 4.3 check whether pastLocalDateTime isAfter todaysLocalDateTime
boolean isAfter3 = pastLocalDateTime.isAfter(todaysLocalDateTime);
System.out.println("4.3 Past Date/time (" + pastLocalDateTime
+ ") \n\t\t is After Current Date/time (" + todaysLocalDateTime + ") ? "
+ isAfter3);
// 4.4 check whether pastLocalDateTime isAfter futureLocalDateTime
boolean isAfter4 = pastLocalDateTime.isAfter(futureLocalDateTime);
System.out.println("4.4 Past Date/time (" + pastLocalDateTime
+ ") \n\t\t is After Future Date/time (" + futureLocalDateTime + ") ? "
+ isAfter4);
// 4.5 check whether futureLocalDateTime isAfter todaysLocalDateTime
boolean isAfter5 = futureLocalDateTime.isAfter(todaysLocalDateTime);
System.out.println("4.5 Future Date/time (" + futureLocalDateTime
+ ") \n\t\t is After Current Date/time (" + todaysLocalDateTime + ") ? "
+ isAfter5);
// 4.6 check whether futureLocalDateTime isAfter pastLocalDateTime
boolean isAfter6 = futureLocalDateTime.isAfter(todaysLocalDateTime);
System.out.println("4.6 Future Date/time (" + futureLocalDateTime
+ ") \n\t\t is After Past Date/time (" + pastLocalDateTime + ") ? "
+ isAfter6);
// 5. isAfter() - self LocalDateTime comparison
System.out.println("\n\n5. Self comparing LocalDateTime using isAfter() method :- \n");
// 5.1 check whether todaysLocalDateTime isAfter todaysLocalDateTime
boolean isAfter7 = todaysLocalDateTime.isAfter(todaysLocalDateTime);
System.out.println("5.1 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is After Current Date/time (" + todaysLocalDateTime + ") ? "
+ isAfter7);
// 5.2 check whether pastLocalDateTime isAfter pastLocalDateTime
boolean isAfter8 = pastLocalDateTime.isAfter(pastLocalDateTime);
System.out.println("5.2 Past Date/time (" + pastLocalDateTime
+ ") \n\t\t is After Past Date/time (" + pastLocalDateTime + ") ? "
+ isAfter8);
// 5.3 check whether futureLocalDateTime isAfter futureLocalDateTime
boolean isAfter9 = futureLocalDateTime.isAfter(futureLocalDateTime);
System.out.print("5.3 Future Date/time (" + futureLocalDateTime
+ ") \n\t\t is After Future Date/time (" + futureLocalDateTime + ") ? "
+ isAfter9);
}
}
Output:
1. Current Date/time is = 2022-08-11T12:30:30.000000500
2. Past Date/time is = 2022-08-11T01:01:01.000000100
3. Future Date/time is = 2022-08-11T23:59:59.000000200
4. Comparing 2 LocalDateTime using isAfter() method :-
4.1 Current Date/time (2022-08-11T12:30:30.000000500)
is After Future Date/time (2022-08-11T23:59:59.000000200) ? false
4.2 Current Date/time (2022-08-11T12:30:30.000000500)
is After Past Date/time (2022-08-11T01:01:01.000000100) ? true
4.3 Past Date/time (2022-08-11T01:01:01.000000100)
is After Current Date/time (2022-08-11T12:30:30.000000500) ? false
4.4 Past Date/time (2022-08-11T01:01:01.000000100)
is After Future Date/time (2022-08-11T23:59:59.000000200) ? false
4.5 Future Date/time (2022-08-11T23:59:59.000000200)
is After Current Date/time (2022-08-11T12:30:30.000000500) ? true
4.6 Future Date/time (2022-08-11T23:59:59.000000200)
is After Past Date/time (2022-08-11T01:01:01.000000100) ? true
5. Self comparing LocalDateTime using isAfter() method :-
5.1 Current Date/time (2022-08-11T12:30:30.000000500)
is After Current Date/time (2022-08-11T12:30:30.000000500) ? false
5.2 Past Date/time (2022-08-11T01:01:01.000000100)
is After Past Date/time (2022-08-11T01:01:01.000000100) ? false
5.3 Future Date/time (2022-08-11T23:59:59.000000200)
is After Future Date/time (2022-08-11T23:59:59.000000200) ? false
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoLocalDateTime.html
Happy Coding !!
Happy Learning !!