HowToDoInJava

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

Compare LocalDate instances

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

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

The recommended way to compare two localdate objects is using provided methods which compare both date objects and return a boolean value – true or false.

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

public class LocalDateComparison 
{
	public static void main(String[] args) 
	{
		LocalDate nineApr = LocalDate.parse("2019-04-09");
		
		LocalDate fourApr = LocalDate.parse("2019-04-04");
		
		boolean isBefore = nineApr.isBefore(fourApr);
		System.out.println("nineApr is before fourApr :: " + isBefore);
		
		boolean isAfter = nineApr.isAfter(fourApr);
		System.out.println("nineApr is after fourApr :: " + isAfter);
		
		boolean isEqual = nineApr.isEqual(LocalDate.of(2019, 4, 9));
		System.out.println("nineApr is equal to 04-09 :: " + isEqual);
	}
}

Program output.

nineApr is before fourApr :: false
nineApr is after fourApr :: true
nineApr is equal to 04-09 :: true

2. LocalDate compareTo() method

The method compareTo() compares two local dates and returns an integer value based on the comparison.

public int compareTo(ChronoLocalDate otherDate)
  • 0 if both the dates represent the same date in calendar.
  • positive integer if given date is later than the otherDate.
  • negative integer if given date is earlier than the otherDate.
import java.time.LocalDate;

public class LocalDateComparison 
{
	public static void main(String[] args) 
	{
		LocalDate nineApr = LocalDate.parse("2019-04-09");
		
		LocalDate fourApr = LocalDate.parse("2019-04-04");
		
		int compareValue = nineApr.compareTo(fourApr);
		
		if(compareValue > 0) 
		{
			System.out.println("nineApr is later than fourApr");
		} 
		else if (compareValue < 0) 
		{
			System.out.println("nineApr is earlier than fourApr");
		} 
		else 
		{
			System.out.println("both dates are equal");
		}
	}
}

Program output.

nineApr is later than fourApr

3. LocalDate equals() method

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

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

public class LocalDateComparison 
{
	public static void main(String[] args) 
	{
		System.out.println( LocalDate.parse("2019-04-09")
								.equals(LocalDate.of(2019, 4, 9)) );	//true
		
		System.out.println( LocalDate.parse("2019-04-09")
								.equals(LocalDate.of(2019, 1, 1)) );	//false
	}
}

Program output.

true
false

Happy Learning !!

Ref : LocalDate 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