In this article, we will discuss different ways to compare two instances of LocalDateTime in Java 1.8 version
1. Comparing two LocalDateTime instances :
There are different methods available to compare 2 LocalDateTime instances, those are –
- compareTo(ChronoLocalDateTime) –
- This method compares invoking LocalDateTime with another LocalDateTime
- isBefore(ChronoLocalDateTime) –
- This method checks if invoking LocalDateTime is before the specified LocalDateTime
- isAfter(ChronoLocalDateTime) –
- This method checks if invoking LocalDateTime is after the specified LocalDateTime
- isEqual(ChronoLocalDateTime) –
- This method checks if invoking LocalDateTime is equal to the specified LocalDateTime
- equals(Object) –
- This method checks if invoking LocalDateTime is equal to another LocalDateTime
2. Examples for Comparing 2 LocalDateTime instances :
- Using compareTo(ChronoLocalDateTime) method
- Using isBefore(ChronoLocalDateTime) method
- Using isAfter(ChronoLocalDateTime) method
- Using isEqual(ChronoLocalDateTime) method
- Using equals(Object) method
2.1 Using compareTo(ChronoLocalDateTime) method :
- This method compares invoking LocalDateTime with another LocalDateTime and returns integer value based on the comparison
- Returns Zero, if both LocalDateTime in comparison are equal
- Returns Positive value, if invoking LocalDateTime is latter-than (greater-than) the specified LocalDateTime
- Returns Negative value, if invoking LocalDateTime is earlier-than (lesser-than) the specified LocalDateTime
Compare2LocalDateTimeUsingCompareToMethod.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.Month;
public class Compare2LocalDateTimeUsingCompareToMethod {
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. LocalDateTime comparison using compareTo() method
System.out.println("\n\n4. Comparison with compareTo() method :- \n");
// 4.1 check whether todaysLocalDateTime Older-than futureLocalDateTime
int compareTo1 = todaysLocalDateTime.compareTo(futureLocalDateTime);
System.out.println("4.1 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is Older-than Future Date/time (" + futureLocalDateTime + ") ? "
+ (compareTo1 < 0));
// 4.2 check whether todaysLocalDateTime Latter-than futureLocalDateTime
int compareTo2 = todaysLocalDateTime.compareTo(futureLocalDateTime);
System.out.println("4.2 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is Latter-than Future Date/time (" + futureLocalDateTime + ") ? "
+ (compareTo2 > 0));
// 4.3 check whether todaysLocalDateTime Older-than pastLocalDateTime
int compareTo3 = todaysLocalDateTime.compareTo(pastLocalDateTime);
System.out.println("4.3 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is Older than Past Date/time (" + pastLocalDateTime + ") ? "
+ (compareTo3 < 0));
// 4.4 check whether todaysLocalDateTime Latter-than pastLocalDateTime
int compareTo4 = todaysLocalDateTime.compareTo(pastLocalDateTime);
System.out.print("4.4 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is Latter-than Past Date/time (" + pastLocalDateTime + ") ? "
+ (compareTo4 > 0));
}
}
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. Comparison with compareTo() method :-
4.1 Current Date/time (2022-08-11T12:30:30.000000500)
is Older-than Future Date/time (2022-08-11T23:59:59.000000200) ? true
4.2 Current Date/time (2022-08-11T12:30:30.000000500)
is Latter-than Future Date/time (2022-08-11T23:59:59.000000200) ? false
4.3 Current Date/time (2022-08-11T12:30:30.000000500)
is Older than Past Date/time (2022-08-11T01:01:01.000000100) ? false
4.4 Current Date/time (2022-08-11T12:30:30.000000500)
is Latter-than Past Date/time (2022-08-11T01:01:01.000000100) ? true
2.2 Using isBefore(ChronoLocalDateTime) method :
- This method checks if invoking LocalDateTime is before the specified LocalDateTime
- Returns true, if invoking LocalDateTime is before the specified LocalDateTime
- Returns false, if invoking LocalDateTime is not before the specified LocalDateTime
- Read Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ? for more details and examples
CheckLocalDateTimeIsBeforeAnotherLocalDateTime.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.Month;
public class CheckLocalDateTimeIsBeforeAnotherLocalDateTime {
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. isBefore() - LocalDateTime comparison
System.out.println("\n\n4. Comparing 2 LocalDateTime using isBefore() method :- \n");
// 4.1 check whether todaysLocalDateTime isBefore futureLocalDateTime
boolean isBefore1 = todaysLocalDateTime.isBefore(futureLocalDateTime);
System.out.println("4.1 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is Before Future Date/time (" + futureLocalDateTime + ") ? "
+ isBefore1);
// 4.2 check whether todaysLocalDateTime isBefore pastLocalDateTime
boolean isBefore2 = todaysLocalDateTime.isBefore(pastLocalDateTime);
System.out.println("4.2 Current Date/time (" + todaysLocalDateTime
+ ") \n\t\t is Before Past Date/time (" + pastLocalDateTime + ") ? "
+ isBefore2);
// 4.3 check whether pastLocalDateTime isBefore todaysLocalDateTime
boolean isBefore3 = pastLocalDateTime.isBefore(todaysLocalDateTime);
System.out.println("4.3 Past Date/time (" + pastLocalDateTime
+ ") \n\t\t is Before Current Date/time (" + todaysLocalDateTime + ") ? "
+ isBefore3);
// 4.4 check whether pastLocalDateTime isBefore futureLocalDateTime
boolean isBefore4 = pastLocalDateTime.isBefore(futureLocalDateTime);
System.out.println("4.4 Past Date/time (" + pastLocalDateTime
+ ") \n\t\t is Before Future Date/time (" + futureLocalDateTime + ") ? "
+ isBefore4);
// 4.5 check whether futureLocalDateTime isBefore todaysLocalDateTime
boolean isBefore5 = futureLocalDateTime.isBefore(todaysLocalDateTime);
System.out.println("4.5 Future Date/time (" + futureLocalDateTime
+ ") \n\t\t is Before Current Date/time (" + todaysLocalDateTime + ") ? "
+ isBefore5);
// 4.6 check whether futureLocalDateTime isBefore pastLocalDateTime
boolean isBefore6 = futureLocalDateTime.isBefore(todaysLocalDateTime);
System.out.print("4.6 Future Date/time (" + futureLocalDateTime
+ ") \n\t\t is Before Past Date/time (" + pastLocalDateTime + ") ? "
+ isBefore6);
}
}
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 isBefore() method :-
4.1 Current Date/time (2022-08-11T12:30:30.000000500)
is Before Future Date/time (2022-08-11T23:59:59.000000200) ? true
4.2 Current Date/time (2022-08-11T12:30:30.000000500)
is Before Past Date/time (2022-08-11T01:01:01.000000100) ? false
4.3 Past Date/time (2022-08-11T01:01:01.000000100)
is Before Current Date/time (2022-08-11T12:30:30.000000500) ? true
4.4 Past Date/time (2022-08-11T01:01:01.000000100)
is Before Future Date/time (2022-08-11T23:59:59.000000200) ? true
4.5 Future Date/time (2022-08-11T23:59:59.000000200)
is Before Current Date/time (2022-08-11T12:30:30.000000500) ? false
4.6 Future Date/time (2022-08-11T23:59:59.000000200)
is Before Past Date/time (2022-08-11T01:01:01.000000100) ? false
2.3 Using isAfter(ChronoLocalDateTime) method :
- This method checks if invoking LocalDateTime is after the specified LocalDateTime
- Returns true, if invoking LocalDateTime is after the specified LocalDateTime
- Returns false, if invoking LocalDateTime is not after the specified LocalDateTime
- Read Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ? for more details and examples
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.print("4.6 Future Date/time (" + futureLocalDateTime
+ ") \n\t\t is After Past Date/time (" + pastLocalDateTime + ") ? "
+ isAfter6);
}
}
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
2.4 Using isEqual(ChronoLocalDateTime) method :
- This method checks if invoking LocalDateTime is equal to the specified LocalDateTime
- Return true, only if both invoking LocalDateTime & specified LocalDateTime are same
- Return false, for all other cases
CheckTwoLocalDateTimeAreEqualUsingIsEqualMethod.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.Month;
public class CheckTwoLocalTimeDateAreEqualUsingIsEqualMethod {
public static void main(String[] args) {
// 1. get Current Date/time 1
LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
System.out.println("1. Today's Date/time is = " + todaysLocalDateTime);
// 2. get Current Date/time 2
LocalDateTime currentLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
System.out.println("2. Current Date/time is = " + currentLocalDateTime);
// 3. 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);
// 4. 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);
// 5. LocalDateTime comparison using isEqual() method
System.out.println("\n\n5. Check 2 LocalDateTime are equal using isEqual() method :- \n");
// 5.1 check whether todaysLocalDateTime & todaysLocalDateTime are same ?
boolean isEqual1 = todaysLocalDateTime.isEqual(todaysLocalDateTime);
System.out.println("5.1 Today's LocalDateTime (" + todaysLocalDateTime
+ ") \n\t\t & Current LocalDateTime (" + todaysLocalDateTime + ") are equal ?\t "
+ isEqual1);
// 5.2 check whether todaysLocalDateTime & pastLocalDateTime are same ?
boolean isEqual2 = todaysLocalDateTime.isEqual(pastLocalDateTime);
System.out.println("5.2 Today's LocalDateTime (" + todaysLocalDateTime
+ ") \n\t\t & Past LocalDateTime (" + pastLocalDateTime + ") are equal ?\t "
+ isEqual2);
// 5.3 check whether todaysLocalDateTime & futureLocalDateTime are same ?
boolean isEqual3 = todaysLocalDateTime.isEqual(futureLocalDateTime);
System.out.print("5.3 Today's LocalDateTime (" + todaysLocalDateTime
+ ") \n\t\t & Future LocalDateTime (" + futureLocalDateTime + ") are equal ?\t "
+ isEqual3);
}
}
Output:
1. Today's Date/time is = 2022-08-11T12:30:30.000000500
2. 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
5. Check 2 LocalDateTime are equal using isEqual() method :-
5.1 Today's LocalDateTime (2022-08-11T12:30:30.000000500)
& Current LocalDateTime (2022-08-11T12:30:30.000000500) are equal ? true
5.2 Today's LocalDateTime (2022-08-11T12:30:30.000000500)
& Past LocalDateTime (2022-08-11T01:01:01.000000100) are equal ? false
5.3 Today's LocalDateTime (2022-08-11T12:30:30.000000500)
& Future LocalDateTime (2022-08-11T23:59:59.000000200) are equal ? false
2.5 Using equals(Object) method :
- This method checks if invoking LocalDateTime is equal to the another LocalDateTime
- Return true, only if both invoking LocalDateTime & specified LocalDateTime are same
- Return false, for all other cases
CheckTwoLocalDateTimeAreEqualUsingEqualsMethod.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
import java.time.Month;
public class CheckTwoLocalDateTimeAreEqualUsingEqualsMethod {
public static void main(String[] args) {
// 1. get Current Date/time 1
LocalDateTime todaysLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
System.out.println("1. Today's Date/time is = " + todaysLocalDateTime);
// 2. get Current Date/time 2
LocalDateTime currentLocalDateTime = LocalDateTime.of(2022, Month.AUGUST, 11, 12, 30, 30, 500);
System.out.println("2. Current Date/time is = " + currentLocalDateTime);
// 3. 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);
// 4. 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);
// 5. LocalDateTime comparison using isEqual() method
System.out.println("\n\n5. Check 2 LocalDateTime are equal using isEqual() method :- \n");
// 5.1 check whether todaysLocalDateTime & todaysLocalDateTime are same ?
boolean isEqual1 = todaysLocalDateTime.equals(todaysLocalDateTime);
System.out.println("5.1 Today's LocalDateTime (" + todaysLocalDateTime
+ ") \n\t\t & Current LocalDateTime (" + todaysLocalDateTime + ") are equal ?\t "
+ isEqual1);
// 5.2 check whether todaysLocalDateTime & pastLocalDateTime are same ?
boolean isEqual2 = todaysLocalDateTime.equals(pastLocalDateTime);
System.out.println("5.2 Today's LocalDateTime (" + todaysLocalDateTime
+ ") \n\t\t & Past LocalDateTime (" + pastLocalDateTime + ") are equal ?\t "
+ isEqual2);
// 5.3 check whether todaysLocalDateTime & futureLocalDateTime are same ?
boolean isEqual3 = todaysLocalDateTime.equals(futureLocalDateTime);
System.out.print("5.3 Today's LocalDateTime (" + todaysLocalDateTime
+ ") \n\t\t & Future LocalDateTime (" + futureLocalDateTime + ") are equal ?\t "
+ isEqual3);
}
}
Output:
1. Today's Date/time is = 2022-08-11T12:30:30.000000500
2. 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
5. Check 2 LocalDateTime are equal using isEqual() method :-
5.1 Today's LocalDateTime (2022-08-11T12:30:30.000000500)
& Current LocalDateTime (2022-08-11T12:30:30.000000500) are equal ? true
5.2 Today's LocalDateTime (2022-08-11T12:30:30.000000500)
& Past LocalDateTime (2022-08-11T01:01:01.000000100) are equal ? false
5.3 Today's LocalDateTime (2022-08-11T12:30:30.000000500)
& Future LocalDateTime (2022-08-11T23:59:59.000000200) are equal ? 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 !!