Using equals()
LocalDate does override equals:
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;
}
If you are not happy with the result of equals(), you are good using the predefined methods of LocalDate.
isAfter()isBefore()isEqual()
Notice that all of those method are using the compareTo0() method and just check the cmp value. if you are still getting weird result (which you shouldn't), please attach an example of input and output
Top answer 1 of 4
139
Using equals()
LocalDate does override equals:
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;
}
If you are not happy with the result of equals(), you are good using the predefined methods of LocalDate.
isAfter()isBefore()isEqual()
Notice that all of those method are using the compareTo0() method and just check the cmp value. if you are still getting weird result (which you shouldn't), please attach an example of input and output
2 of 4
6
LocalDate ld ....;
LocalDateTime ldtime ...;
ld.isEqual(LocalDate.from(ldtime));
Baeldung
baeldung.com › home › java › java dates › comparing dates in java
Comparing Dates in Java | Baeldung
July 17, 2024 - Although both ZonedDateTime instances represent the same moment in time, they do not represent equal Java objects. They have different LocalDateTime and ZoneId fields internally: assertThat(timeInNewYork.equals(timeInBerlin), is(false)); assertThat(timeInNewYork.compareTo(timeInBerlin), is(-1));
LabEx
labex.io › tutorials › java-how-to-compare-two-localdate-instances-in-java-415184
How to compare two LocalDate instances in Java | LabEx
The LocalDate class provides a wide range of methods for working with dates. Some of the commonly used methods include: getDayOfWeek(): Returns the day of the week as a DayOfWeek enum. ... These methods provide a powerful set of tools for working with dates in your Java applications. Comparing LocalDate objects is a common operation in Java programming.
BeginnersBook
beginnersbook.com › 2017 › 10 › java-localdate-compareto-method-example
Java LocalDate – compareTo() method example
The method compareTo() compares two dates and returns an integer value based on the comparison. ... It returns 0 if both the dates are equal. It returns positive value if “this date” is greater than the otherDate. It returns negative value if “this date” is less than the otherDate. ...
Mkyong
mkyong.com › home › java › how to compare dates in java
How to compare dates in Java - Mkyong.com
March 26, 2021 - For the new Java 8 java.time.* classes, all contains similar compareTo, isBefore(), isAfter() and isEqual() to compare two dates, and it works the same way. java.time.LocalDate – date without time, no time-zone.
Scaler
scaler.com › home › topics › compare two dates in java
Compare Two Dates in Java - Scaler Topics
April 21, 2024 - Here we are using DateTimeFormatter ... LocalDate class to compare the dates. To compare Dates in Java, we can use the Date, Calendar, or LocalDate class....
Oracle
docs.oracle.com › javase › 8 › docs › api › java › time › LocalDate.html
LocalDate (Java Platform SE 8 )
October 20, 2025 - Only objects of type LocalDate are compared, other types return false. To compare the dates of two TemporalAccessor instances, including dates in two different chronologies, use ChronoField.EPOCH_DAY as a comparator. ... A hash code for this date. ... Outputs this date as a String, such as 2007-12-03. The output will be in the ISO-8601 format uuuu-MM-dd. ... Java...
How to do in Java
howtodoinjava.com › home › java date time › compare two localdatetime instances
Compare Two LocalDateTime Instances - HowToDoInJava
December 14, 2022 - 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)); //true System.out.println(ldt1.equals(ldt3)); //false · Happy Learning !! ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
Apache
lists.apache.org › thread › bwpht6hx2hyr4nk2ftxpk2tmbp09sv2c
comparing LocalDate and LocalDateTime using built-in ...
Email display mode: · Modern rendering · Legacy rendering · This site requires JavaScript enabled. Please enable it
TutorialsPoint
tutorialspoint.com › localdate-compareto-method
LocalDate compareTo() method
the LocalDate object to be compared. If the first LocalDate object is greater than the second LocalDate object it returns a positive number, if the first LocalDate object is lesser than the second LocalDate object it returns a negative number and if both the LocalDate objects are equal it returns ...
TutorialsPoint
tutorialspoint.com › javatime › javatime_localdate_compareto.htm
java.time.LocalDate.compareTo() Method Example
package com.tutorialspoint; import java.time.LocalDate; public class LocalDateDemo { public static void main(String[] args) { LocalDate date = LocalDate.parse("2017-02-03"); System.out.println(date); LocalDate date1 = LocalDate.parse("2017-03-03"); System.out.println(date1); System.out.println(date1.compareTo(date)); } }
BeginnersBook
beginnersbook.com › 2017 › 10 › java-localdate-equals
Java LocalDate – equals() method example
Strings are parsed * into LocalDate instances and then compared against each other */ LocalDate date1 = LocalDate.parse("2017-10-21"); System.out.println(date1); LocalDate date2 = LocalDate.parse("2017-08-23"); System.out.println(date2); System.out.println(date1.equals(date2)); } } Output: 2017-10-28 2017-10-28 true 2017-10-21 2017-08-23 false · Java LocalDate ·
Benchresources
benchresources.net › home › java › java 8 – how to compare two localdate instances ?
Java 8 – How to compare two LocalDate instances ? - BenchResources.Net
August 10, 2022 - Returns Zero, if both LocalDate in comparison are equal · Returns Positive value, if invoking LocalDate is latter-than (greater-than) the specified LocalDate · Returns Negative value, if invoking LocalDate is earlier-than (lesser-than) the specified LocalDate · package in.bench.resources.java8.localdate.examples; import java.time.LocalDate; import java.time.Month; public class Compare2LocalDateUsingCompareToMethod { public static void main(String[] args) { // 1.
Mkyong
mkyong.com › home › java8 › java 8 – difference between two localdate or localdatetime
Java 8 - Difference between two LocalDate or LocalDateTime - Mkyong.com
February 12, 2020 - package com.mkyong.java8; import java.time.Duration; import java.time.LocalDateTime; public class JavaLocalDateTime { public static void main(String[] args) { LocalDateTime from = LocalDateTime.of(2020, 10, 4, 10, 20, 55); LocalDateTime to = LocalDateTime.of(2020, 10, 10, 10, 21, 1); Duration duration = Duration.between(from, to); // days between from and to System.out.println(duration.toDays() + " days"); // hours between from and to System.out.println(duration.toHours() + " hours"); // minutes between from and to System.out.println(duration.toMinutes() + " minutes"); // seconds between from and to System.out.println(duration.toSeconds() + " seconds"); System.out.println(duration.getSeconds() + " seconds"); } }
Stack Abuse
stackabuse.com › how-to-compare-dates-in-java
How to Compare Dates in Java
February 28, 2023 - The DateTimeComparator class is specifically made to compare dates. It works with ReadableInstants, Strings, Dates, Calendars and Longs. So, we'll have to convert our Joda-Time LocalDates, to Dates:
Studytonight
studytonight.com › java-examples › java-localdate-compareto-method
Java LocalDate CompareTo() Method - Studytonight
In this example we have two dates and using compareTo() to check whether the specified date is older to other on not. import java.time.LocalDate; public class Demo { public static void main(String[] args){ // Take a date LocalDate localDate1 = LocalDate.of(2018, 2, 20); // Displaying date System.out.println("First Date is : "+localDate1); // Take another date LocalDate localDate2 = LocalDate.of(2018, 2, 22); // Displaying date System.out.println("Another Date is : "+localDate2); // Using compareTo() method int val = localDate1.compareTo(localDate2); System.out.println(val); } }