Javaexercise.com

How To Compare Two Dates In Java 8 And Higher Versions?

<p>Java <strong>compareTo() method of LocalDate</strong> class is used to compare a date with another date.</p> <p>If dates are instances of&nbsp;LocalDate class, then the comparison will be&nbsp;based on the date. If some dates being compared are in different chronologies, then the chronology is also considered.</p> <p>This method is specified in two interfaces <strong>java.time.chrono.ChronoLocalDate</strong> and <strong>java.lang.Comparable.</strong></p> <p>The compareTo() method returns an integer value that can be positive or negative or zero.</p> <p>In this topic, we will learn&nbsp;<strong>to compare two dates</strong>&nbsp;with examples. The Syntax of the method is given below.</p> <h2>Syntax</h2> <pre> <code class="language-java">public int compareTo(ChronoLocalDate otherdate) </code></pre> <p>This method belongs to&nbsp;<strong>Java 8 LocalDate class</strong>&nbsp;which is stored in&nbsp;<strong>java.time</strong>&nbsp;package and method description are given below.</p> <h3>Parameters:</h3> <p><strong>otherdate:</strong> Instance of another date to be compared.</p> <h3>Return Value:</h3> <ul> <li> <p><strong>Zero</strong>, if both dates are equal.</p> </li> <li> <p>A<strong> negative value</strong>, if&nbsp;one date is less than the other.</p> </li> <li> <p>A<strong> positive value</strong>, if one date is greater than the other.</p> </li> </ul> <h3>Exception:</h3> <p><strong>No</strong> Exception</p> <h3>Version:</h3> <p>This method is added to <strong>Java 1.8</strong>.</p> <h2>How to Compare two different Dates in Java</h2> <p>The below example returns a negative value as a result because date1 is less than date2. See the example here.</p> <pre> <code class="language-java">import java.time.LocalDate; /* * Example to compare two dates */ public class JExercise { public static void main(String[] args) { // Take a date LocalDate date1 = LocalDate.parse("2020-08-17"); // Displaying date System.out.println("First Date is : "+date1); // Take another date LocalDate date2 = LocalDate.parse("2020-08-20"); // Displaying date System.out.println("Second Date is : "+date2); // Using compareTo() method int result = date1.compareTo(date2); System.out.println(result); } }</code></pre> <p><strong>Output:</strong></p> <p>First Date is : 2020-08-17<br /> Second Date is : 2020-08-20<br /> -3<br /> &nbsp;</p> <h2>Example: Compare Two Dates in Java</h2> <p>The below&nbsp;example compares two dates and returns a positive value because date1 is greater than date2. Similarly, it returns 0 when date2 and date3 are compared.</p> <pre> <code class="language-java">import java.time.LocalDate; /* * Example to compare two dates */ public class JExercise { public static void main(String[] args) { // Take a date LocalDate date1 = LocalDate.parse("2020-08-22"); // Displaying date System.out.println("First Date is : "+date1); // Take another date LocalDate date2 = LocalDate.parse("2020-08-20"); // Displaying date System.out.println("Second Date is : "+date2); // Using compareTo() method int result = date1.compareTo(date2); System.out.println(result); // If both dates are equal LocalDate date3 = LocalDate.parse("2020-08-20"); result = date2.compareTo(date3); System.out.println(result); } }</code></pre> <p><strong>Output:</strong></p> <p>First Date is : 2020-08-22<br /> Second Date is : 2020-08-20<br /> 2<br /> 0</p> <h2>Conclusion</h2> <p>Well, in this topic, we have learned&nbsp;To compare two dates by using the <strong>compareTo()</strong> method of <strong>Java 8 LocalDate</strong> class.</p> <p>If we missed something, you can suggest us at - <strong>info.javaexercise@gmail.com</strong></p>