Comparing Two LocalDateTime Instances

Learn to compare two LocalDateTime instances to find out which date represents an older timestamp in comparison to another timestamp. LocalDateTime class is part of java.time package added in Java 8.

1. isAfter(), isBefore() and isEqual() Methods

The recommended way to compare two LocalDateTime objects is using provided methods which compare both date-time parts and return a boolean value – true or false. These methods only consider the position of the two dates on the local timeline and do not consider the chronology, or calendar system.

  • boolean isAfter( otherDateTime ) – Checks if given date-time is after the other date-time.
  • boolean isBefore( otherDateTime ) – Checks if given date-time is before the other date-time.
  • boolean isEqual( otherDateTime ) – Checks if given date-time is equals to the other date-time.
LocalDateTime now = LocalDateTime.now();
LocalDateTime pastDate = LocalDateTime
	.parse("2017-01-14T15:32:56.000");

boolean isBefore = now.isBefore(pastDate);	//false
	
boolean isAfter = now.isAfter(pastDate);	//true

boolean isEqual = now.isEqual(pastDate);	//false20

2. LocalDateTime compareTo() Method

The method compareTo() compares two local date-time objects and returns an integer value based on the comparison. These methods compare the instances without considering the timezone or calendar information.

The int compareTo(otherDateTime) method returns:

  • 0 (Zero) if both the date-times represent the same time instance of the day.
  • Positive integer if given date-times is later than the otherDate.
  • Negative integer if given date-times is earlier than the otherDate.
LocalDateTime ldt1 = LocalDateTime.parse("2019-04-28T22:32:38.536");
LocalDateTime ldt2 = LocalDateTime.parse("2017-01-14T15:32:56.000");

int diff = ldt1.compareTo(ldt2);

System.out.println("Compare value = " + diff);	//2

if (diff > 0) {
  System.out.println("ldt1 is later than ldt2");	//Prints it
} else if (diff < 0) {
  System.out.println("ldt1 is earlier than ldt2");	
} else {
  System.out.println("both dates are equal");
}

3. LocalDateTime equals() Method

If we want to only check if both date and time values are equal or not (i.e. represent the same time of the day in the local timeline), we can use equals() method. This method returns:

  • true – given date is same as otherDate.
  • false – given date is NOT same as otherDate.
LocalDateTime ldt1 = LocalDateTime.of(2019, 4, 9, 10, 10, 50);
LocalDateTime ldt2 = LocalDateTime.of(2019, 4, 9, 10, 10, 50);
LocalDateTime ldt3 = LocalDateTime.of(2019, 4, 9, 11, 12, 50);

System.out.println(ldt1.equals(ldt2));  //true
System.out.println(ldt1.equals(ldt3));  //false

Happy Learning !!

Sourcecode on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial