Java LocalDate Class | compareTo() Method with Example

LocalDate Class compareTo() method: Here, we are going to learn about the compareTo() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on May 29, 2020

LocalDate Class compareTo() method

  • compareTo() method is available in java.time package.
  • compareTo() method is used to compare this LocalDate object to the given object.
  • compareTo() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • compareTo() method does not throw an exception at the time of comparing.

Syntax:

    public int compareTo(ChronoLocalDate l_date);

Parameter(s):

  • ChronoLocalDate l_date – represents the object to be compared to this LocalDate.

Return value:

The return type of this method is int, it may return anyone of the given values,

  • When (this LocalDate) < (ChronoLocalDate l_date), it returns -ve.
  • When (this LocalDate) > (ChronoLocalDate l_date), it returns +ve.
  • When (this LocalDate) == (ChronoLocalDate l_date), it returns 0.

Example:

// Java program to demonstrate the example 
// of int compareTo(ChronoLocalDate l_date) 
// method of LocalDate

import java.time.*;

public class CompareToOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.now();

        // Display l_da1,l_da2
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method compares this date object to
        // the given date object i.e. it returns -ve
        // value because l_da1 = 2007-04-04 and l_da2
        // = 2020-05-08 so l_da1 - l_da2 = 2007-2020
        // i.e. -13 will be returned
        int compare = l_da1.compareTo(l_da2);

        // Display compare
        System.out.println("l_da1.compareTo(l_da2): " + compare);

        // Here, this method compares this date object to
        // the given date object i.e. it returns +ve
        // value because l_da1 = 2007-04-04 and l_da2
        // = 2020-05-08 so l_da2 - l_da1 = 2020-2007
        // i.e. 13 will be returned
        compare = l_da2.compareTo(l_da1);

        // Display compare
        System.out.println("l_da2.compareTo(l_da1): " + compare);

        // Here, this method compares this date object to
        // the given date object i.e. it returns 0
        // because l_da1 == l_da1
        compare = l_da1.compareTo(l_da1);

        // Display compare
        System.out.println("l_da1.compareTo(l_da1): " + compare);
    }
}

Output

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-04
l_da2: 2020-05-29

l_da1.compareTo(l_da2): -13
l_da2.compareTo(l_da1): 13
l_da1.compareTo(l_da1): 0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.