In this article, we will learn how to check whether one LocalDate is Before another LocalDate using LocalDate.isBefore() method in Java 1.8 version
Compare 2 LocalDate using isBefore() method :
- If there are 2 LocalDate and the requirement is to check whether one LocalDate is before another LocalDate, then isBefore() method can be used
- isBefore(ChronoLocalDate) – checks if the invoking LocalDate is before the specified LocalDate
- Lets see an example for comparing 2 LocalDate using isBefore() method
- Note: Self comparing LocalDate using isBefore() method always returns false, as shown in the below illustration
CheckLocalDateIsBeforeAnotherLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
import java.time.Month;
public class CheckLocalDateIsBeforeAnotherLocalDate {
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. isBefore() - LocalDate comparison
System.out.println("\n\n4. Comparing 2 LocalDate using isBefore() method :- \n");
// 4.1 check whether todaysLocalDate isBefore futureLocalDate
boolean isBefore1 = todaysLocalDate.isBefore(futureLocalDate);
System.out.println("4.1 Today's LocalDate (" + todaysLocalDate
+ ") is Before Future LocalDate (" + futureLocalDate + ") ? "
+ isBefore1);
// 4.2 check whether todaysLocalDate isBefore pastLocalDate
boolean isBefore2 = todaysLocalDate.isBefore(pastLocalDate);
System.out.println("4.2 Today's LocalDate (" + todaysLocalDate
+ ") is Before Past LocalDate (" + pastLocalDate + ") ? "
+ isBefore2);
// 4.3 check whether pastLocalDate isBefore todaysLocalDate
boolean isBefore3 = pastLocalDate.isBefore(todaysLocalDate);
System.out.println("4.3 Past LocalDate (" + pastLocalDate
+ ") is Before Today's LocalDate (" + todaysLocalDate + ") ? "
+ isBefore3);
// 4.4 check whether pastLocalDate isBefore futureLocalDate
boolean isBefore4 = pastLocalDate.isBefore(futureLocalDate);
System.out.println("4.4 Past LocalDate (" + pastLocalDate
+ ") is Before Future LocalDate (" + futureLocalDate + ") ? "
+ isBefore4);
// 4.5 check whether futureLocalDate isBefore todaysLocalDate
boolean isBefore5 = futureLocalDate.isBefore(todaysLocalDate);
System.out.println("4.5 Future LocalDate (" + futureLocalDate
+ ") is Before Today's LocalDate (" + todaysLocalDate + ") ? "
+ isBefore5);
// 4.6 check whether futureLocalDate isBefore pastLocalDate
boolean isBefore6 = futureLocalDate.isBefore(todaysLocalDate);
System.out.println("4.6 Future LocalDate (" + futureLocalDate
+ ") is Before Past LocalDate (" + pastLocalDate + ") ? "
+ isBefore6);
// 5. isBefore() - self LocalDate comparison
System.out.println("\n\n5. Self comparing LocalDate using isBefore() method :- \n");
// 5.1 check whether todaysLocalDate isBefore todaysLocalDate
boolean isBefore7 = todaysLocalDate.isBefore(todaysLocalDate);
System.out.println("5.1 Today's LocalDate (" + todaysLocalDate
+ ") is Before Today's LocalDate (" + todaysLocalDate + ") ? "
+ isBefore7);
// 5.2 check whether pastLocalDate isBefore pastLocalDate
boolean isBefore8 = pastLocalDate.isBefore(pastLocalDate);
System.out.println("5.2 Past LocalDate (" + pastLocalDate
+ ") is Before Past LocalDate (" + pastLocalDate + ") ? "
+ isBefore8);
// 5.3 check whether futureLocalDate isBefore futureLocalDate
boolean isBefore9 = futureLocalDate.isBefore(futureLocalDate);
System.out.print("5.3 Future LocalDate (" + futureLocalDate
+ ") is Before Future LocalDate (" + futureLocalDate + ") ? "
+ isBefore9);
}
}
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 isBefore() method :-
4.1 Today's LocalDate (2022-08-03) is Before Future LocalDate (2022-08-29) ? true
4.2 Today's LocalDate (2022-08-03) is Before Past LocalDate (2022-08-01) ? false
4.3 Past LocalDate (2022-08-01) is Before Today's LocalDate (2022-08-03) ? true
4.4 Past LocalDate (2022-08-01) is Before Future LocalDate (2022-08-29) ? true
4.5 Future LocalDate (2022-08-29) is Before Today's LocalDate (2022-08-03) ? false
4.6 Future LocalDate (2022-08-29) is Before Past LocalDate (2022-08-01) ? false
5. Self comparing LocalDate using isBefore() method :-
5.1 Today's LocalDate (2022-08-03) is Before Today's LocalDate (2022-08-03) ? false
5.2 Past LocalDate (2022-08-01) is Before Past LocalDate (2022-08-01) ? false
5.3 Future LocalDate (2022-08-29) is Before 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 !!