Close

Java Date Time - LocalDate.isEqual() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDate

java.lang.Objectjava.lang.Objectjava.time.LocalDatejava.time.LocalDatejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateChronoLocalDatejava.io.SerializableSerializableLogicBig

Method:

public boolean isEqual(ChronoLocalDate other)

This method checks if this date is equal to the specified date.

The difference between this method and equals method is only that this method only accepts ChronoLocalDate as parameter, whereas, equals can accept any object.

Difference between this method and compareTo() is, the compareTo method can give you before/after comparison as well. Both methods accepts ChronoLocalDate.

All three methods: isEqualTo(), equals() and compareTo() internally apply the same following logic:

int compareTo0(LocalDate otherDate) {
int cmp = (year - otherDate.year);
if (cmp == 0) {
cmp = (month - otherDate.month);
if (cmp == 0) {
cmp = (day - otherDate.day);
}
}
return cmp;
}



Examples


package com.logicbig.example.localdate;

import java.time.LocalDate;
import java.time.LocalDateTime;

public class IsEqualExample {
//public boolean isEqual(ChronoLocalDate other)

public static void main (String... args) {
LocalDate a = LocalDate.of(2012, 6, 30);
LocalDateTime b = LocalDateTime.of(2012, 6, 30, 12, 30);
System.out.println(a.isEqual(b.toLocalDate()));
System.out.println(a.compareTo(b.toLocalDate()));
System.out.println(a.equals(b));
}
}

Output

true
0
false




See Also