In this article, we will learn how to check whether one LocalDate is After another LocalDate using LocalDate.isAfter() method in Java 1.8 version
Compare 2 LocalDate using isAfter() method :
- If there are 2 LocalDate and the requirement is to check whether one LocalDate is after another LocalDate, then isAfter() method can be used
- isAfter(ChronoLocalDate) – checks if the invoking LocalDate is after the specified LocalDate
- Lets see an example for comparing 2 LocalDate using isAfter() method
- Note: Self comparing LocalDate using isAfter() method always returns false, as shown in the below illustration
CheckLocalDateIsAfterAnotherLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
public class CheckLocalDateIsAfterAnotherLocalDate {
public static void main(String[] args) {
// 1. get today's LocalDate
LocalDate todaysLocalDate = LocalDate.of(2022, Month.AUGUST, 3);
System.out.println("1. Today's LocalDate is = " + todaysLocalDate);
// 2. form past LocalDate
LocalDate pastLocalDate = LocalDate.of(2022, Month.AUGUST, 1);
System.out.println("2. Past LocalDate is = " + pastLocalDate);
// 3. form future LocalDate
LocalDate futureLocalDate = LocalDate.of(2022, Month.AUGUST, 29);
System.out.println("3. Future LocalDate is = " + futureLocalDate);
// 4. isAfter() - LocalDate comparison
System.out.println("\n\n4. Comparing 2 LocalDate using isAfter() method :- \n");
// 4.1 check whether todaysLocalDate isAfter futureLocalDate
boolean isAfter1 = todaysLocalDate.isAfter(futureLocalDate);
System.out.println("4.1 Today's LocalDate (" + todaysLocalDate
+ ") is After Future LocalDate (" + futureLocalDate + ") ? "
+ isAfter1);
// 4.2 check whether todaysLocalDate isAfter pastLocalDate
boolean isAfter2 = todaysLocalDate.isAfter(pastLocalDate);
System.out.println("4.2 Today's LocalDate (" + todaysLocalDate
+ ") is After Past LocalDate (" + pastLocalDate + ") ? "
+ isAfter2);
// 4.3 check whether pastLocalDate isAfter todaysLocalDate
boolean isAfter3 = pastLocalDate.isAfter(todaysLocalDate);
System.out.println("4.3 Past LocalDate (" + pastLocalDate
+ ") is After Today's LocalDate (" + todaysLocalDate + ") ? "
+ isAfter3);
// 4.4 check whether pastLocalDate isAfter futureLocalDate
boolean isAfter4 = pastLocalDate.isAfter(futureLocalDate);
System.out.println("4.4 Past LocalDate (" + pastLocalDate
+ ") is After Future LocalDate (" + futureLocalDate + ") ? "
+ isAfter4);
// 4.5 check whether futureLocalDate isAfter todaysLocalDate
boolean isAfter5 = futureLocalDate.isAfter(todaysLocalDate);
System.out.println("4.5 Future LocalDate (" + futureLocalDate
+ ") is After Today's LocalDate (" + todaysLocalDate + ") ? "
+ isAfter5);
// 4.6 check whether futureLocalDate isAfter pastLocalDate
boolean isAfter6 = futureLocalDate.isAfter(todaysLocalDate);
System.out.println("4.6 Future LocalDate (" + futureLocalDate
+ ") is After Past LocalDate (" + pastLocalDate + ") ? "
+ isAfter6);
// 5. isAfter() - self LocalDate comparison
System.out.println("\n\n5. Self comparing LocalDate using isAfter() method :- \n");
// 5.1 check whether todaysLocalDate isAfter todaysLocalDate
boolean isAfter7 = todaysLocalDate.isAfter(todaysLocalDate);
System.out.println("5.1 Today's LocalDate (" + todaysLocalDate
+ ") is After Today's LocalDate (" + todaysLocalDate + ") ? "
+ isAfter7);
// 5.2 check whether pastLocalDate isAfter pastLocalDate
boolean isAfter8 = pastLocalDate.isAfter(pastLocalDate);
System.out.println("5.2 Past LocalDate (" + pastLocalDate
+ ") is After Past LocalDate (" + pastLocalDate + ") ? "
+ isAfter8);
// 5.3 check whether futureLocalDate isAfter futureLocalDate
boolean isAfter9 = futureLocalDate.isAfter(futureLocalDate);
System.out.print("5.3 Future LocalDate (" + futureLocalDate
+ ") is After Future LocalDate (" + futureLocalDate + ") ? "
+ isAfter9);
}
}
Output:
1. Today's LocalDate is = 2022-08-03
2. Past LocalDate is = 2022-08-01
3. Future LocalDate is = 2022-08-29
4. Comparing 2 LocalDate using isAfter() method :-
4.1 Today's LocalDate (2022-08-03) is After Future LocalDate (2022-08-29) ? false
4.2 Today's LocalDate (2022-08-03) is After Past LocalDate (2022-08-01) ? true
4.3 Past LocalDate (2022-08-01) is After Today's LocalDate (2022-08-03) ? false
4.4 Past LocalDate (2022-08-01) is After Future LocalDate (2022-08-29) ? false
4.5 Future LocalDate (2022-08-29) is After Today's LocalDate (2022-08-03) ? true
4.6 Future LocalDate (2022-08-29) is After Past LocalDate (2022-08-01) ? true
5. Self comparing LocalDate using isAfter() method :-
5.1 Today's LocalDate (2022-08-03) is After Today's LocalDate (2022-08-03) ? false
5.2 Past LocalDate (2022-08-01) is After Past LocalDate (2022-08-01) ? false
5.3 Future LocalDate (2022-08-29) is After Future LocalDate (2022-08-29) ? false
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/chrono/ChronoLocalDate.html
Happy Coding !!
Happy Learning !!