Java LocalDate compareTo Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the compareTo() method from the LocalDate class in Java. This method is used to compare two different dates and returns an integer value.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/abstraction("`Abstraction`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/interface("`Interface`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/abstraction -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/classes_objects -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/date -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/interface -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/modifiers -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/oop -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/packages_api -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/identifier -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/comments -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/data_types -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/if_else -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/operators -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/output -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/strings -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/variables -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} java/system_methods -.-> lab-117776{{"`Java LocalDate compareTo Method`"}} end

Import LocalDate package

Add the following import statement at the beginning of the file to import the LocalDate class:

import java.time.LocalDate;

Compare Dates using compareTo()

In this step, you will compare two different dates using the compareTo() method and display the output based on the comparison. Add the following code to the main() method:

    // Take a date
    LocalDate date1 = LocalDate.of(2021, 12, 31);
    // Displaying date
    System.out.println("Date 1 is : "+date1);
    // Take another date
    LocalDate date2 = LocalDate.now();
    // Displaying date
    System.out.println("Date 2 is : "+date2);

    // Comparing two dates using compareTo()
    int result = date1.compareTo(date2);

    // Display whether date 1 is before, after or equals to date 2
    if(result<0){
      System.out.println("Date 1 is before Date 2");
    }
    else if(result>0){
      System.out.println("Date 1 is after Date 2");
    }
    else{
      System.out.println("Both Dates are same");
    }

Run the code

Save the file and run the following command in the terminal:

javac DateComparator.java && java DateComparator

Change the dates and compare

In this step, you will change the dates used for comparison and see the output. Replace the following line in the code:

LocalDate date1 = LocalDate.of(2021, 12, 31);

With any other date and rerun the code.

Compare same dates

In this step, you will change the dates used for comparison to be the same. Replace the following line in the code:

LocalDate date2 = LocalDate.now();

With the same date as date1 and rerun the code.

Compare today's date with future date

In this step, you will compare today's date with a future date. Replace the following line in the code:

LocalDate date1 = LocalDate.of(2021, 12, 31);

With any date in the future and rerun the code.

Compare today's date with past date

In this step, you will compare today's date with a past date. Replace the following line in the code:

LocalDate date1 = LocalDate.of(2021, 12, 31);

With any date in the past and rerun the code.

Use a different ChronoLocalDate object

In this step, you will use a different object that extends from ChronoLocalDate rather than LocalDate and compare two different dates. First, create a new class named MyDate and add the following code:

import java.time.chrono.ChronoLocalDate;

public class MyDate implements ChronoLocalDate {

  // Add implementation details for ChronoLocalDate interface
  // ...

}

Then, replace the LocalDate variables with MyDate variables and adjust the implementation details for the MyDate class as necessary. Rerun the code to ensure it still works.

Summary

In this lab, you have learned how to compare different dates in Java using the compareTo() method from the LocalDate class. You have also learned how to display output based on the result of the comparison, as well as how to modify the code for different test cases.

Other Java Tutorials you may like