Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Sunday, December 6, 2020

Java LocalDate equals() Example

1. Overview

In this tutorial, We'll learn how to compare two dates in java 8 using LocalDate.equals() method. equals() method returns a boolean value if the dates are same , it returns true else false.

Syntax:
public boolean equals(Object obj)

equals() method compares two dates contents in detail with year, month and day values. If any one of these values are not matched then it consider as these two dates are not equal. 
This method takes Object type argument. This object type should be LocalDate and then only these objects are compared otherwise it returns false directly.

Java LocalDate – equals() Example


2. Java 8 LocalDate.equals() Example


Example program to compare two LocalDate objects in java 8 with the help of equals() method.

Read the inline comments in the below program for better understanding.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;

/**
 * Java 8 LocalDate.equals() method examples
 * 
 * @author javaprograto.com
 *
 */

public class LocalDateEqualsExample {

	public static void main(String[] args) {

		// Creating two LocalDate date objects using now() method
		LocalDate localDate1 = LocalDate.now();
		LocalDate localDate2 = LocalDate.now();

		// printing localDate1 and localDate2
		System.out.println("localDate1 : " + localDate1);
		System.out.println("localDate2 : " + localDate2);

		// LocalDate equals example
		if (localDate1.equals(localDate2)) {
			System.out.println("localDate1 and localDate2 are same");
		} else {
			System.out.println("localDate1 and localDate2 are not same");
		}

		// Creating another two LocalDate date objects using of() method with different dates
		LocalDate localDate3 = LocalDate.of(2025, 01, 01);
		LocalDate localDate4 = LocalDate.of(2030, 01, 01);

		// printing localDate3 and localDate4
		System.out.println("\nlocalDate3 : " + localDate3);
		System.out.println("localDate4 : " + localDate4);

		// LocalDate equals example
		if (localDate3.equals(localDate4)) {
			System.out.println("localDate3 and localDate24 are same");
		} else {
			System.out.println("localDate3 and localDate4 are not same");
		}
	}
}

Output:
localDate1 : 2020-12-06
localDate2 : 2020-12-06
localDate1 and localDate2 are same

localDate3 : 2025-01-01
localDate4 : 2030-01-01
localDate3 and localDate4 are not same

3. Conclusion


In this article, we've seen how to use equals() method of LocalDate class in new datetime api in java 8 with example programs.

If we pass the LocalDateTime object to this equals() method then it returns false because internal implementation checks the given instance is type of LocalDate. If we pass LocalDateTime object, instance type check becomes false.

Alternative to this method, We can use the LocalDate.compareTo() method which compares the dates but compareTo() returns integer value rather than boolean.



No comments:

Post a Comment

Please do not add any spam links in the comments section.