Compare LocalDateTime instances

Learn to compare two LocalDateTime instances to find out which date represents an older timestamp in comparison to other 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 objects and return a boolean value – true or false.

  • boolean isAfter(LocalDateTime otherDateTime) – Checks if given date-time is after the other date-time.
  • boolean isBefore(LocalDateTime otherDateTime) – Checks if given date-time is before the other date-time.
  • boolean isEqual(LocalDateTime otherDateTime) – Checks if given date-time is equals to the other date-time.
import java.time.LocalDateTime;

public class LocalDateTimeComparison 
{
	public static void main(String[] args) 
	{
		LocalDateTime dateTimeOne = LocalDateTime.parse("2019-04-28T22:32:38.536");
		
		LocalDateTime dateTimeTwo = LocalDateTime.parse("2017-01-14T15:32:56.000");
		
		boolean isBefore = dateTimeOne.isBefore(dateTimeTwo);
		System.out.println("dateTimeOne is dateTimeTwo fourApr :: " + isBefore);
		
		boolean isAfter = dateTimeOne.isAfter(dateTimeTwo);
		System.out.println("dateTimeOne is after dateTimeTwo :: " + isAfter);
		
		boolean isEqual = dateTimeOne.isEqual(dateTimeTwo);
		System.out.println("dateTimeOne is equal to dateTimeTwo :: " + isEqual);
	}
}

Program output.

dateTimeOne is dateTimeTwo fourApr :: false
dateTimeOne is after dateTimeTwo :: true
dateTimeOne is equal to dateTimeTwo :: false

2. LocalDateTime compareTo() method

The method compareTo() compares two local date-time objects and returns an integer value based on the comparison.

public int compareTo(ChronoLocalDateTime otherDateTime)
  • 0 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.
import java.time.LocalDateTime;

public class LocalDateTimeComparison 
{
	public static void main(String[] args) 
	{
		LocalDateTime dateTimeOne = LocalDateTime.parse("2019-04-28T22:32:38.536");
		
		LocalDateTime dateTimeTwo = LocalDateTime.parse("2017-01-14T15:32:56.000");
		
		int compareValue = dateTimeOne.compareTo(dateTimeTwo);
		
		System.out.println("Compare value = " + compareValue);
		
		if(compareValue > 0) 
		{
			System.out.println("dateTimeOne is later than dateTimeTwo");
		} 
		else if (compareValue < 0) 
		{
			System.out.println("dateTimeOne is earlier than dateTimeTwo");
		} 
		else 
		{
			System.out.println("both dates are equal");
		}
	}
}

Program output.

Compare value = 2
dateTimeOne is later than dateTimeTwo

3. LocalDateTime equals() method

If we want to only check if both dates are equal of not (i.e. represent same time of the day or not), we can use equals() method.

boolean equals(LocalDateTime otherDateTime)
  • true – given date is same as otherDate.
  • false – given date is NOT same as otherDate.
import java.time.LocalDateTime;

public class LocalDateTimeComparison 
{
	public static void main(String[] args) 
	{
		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));
		System.out.println(ldt1.equals(ldt3));
	}
}

Program output.

true
false

Happy Learning !!

Ref : LocalDateTime Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.