HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Compare LocalDateTime instances

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces