Java LocalDateTime Class | compareTo() Method with Example

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

LocalDateTime Class compareTo() method

  • compareTo() method is available in java.time package.
  • compareTo() method is used to compare this date-time object to the given date-time 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(ChronoLocalDateTime l_datetime) ;

Parameter(s):

  • ChronoLocalDateTime l_datetime – represents the object to be compared to this LocalDateTime.

Return value:

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

  • When (this LocalDateTime) < (ChronoLocalDateTime l_datetime), it returns -1.
  • When (this LocalDateTime) > (ChronoLocalDateTime l_datetime), it returns 1.

Example:

// Java program to demonstrate the example 
// of int compareTo(ChronoLocalDateTime l_datetime) 
// method of LocalDateTime

import java.time.*;

public class CompareToOfLocalDateTime {
    public static void main(String args[]) {
        // Instantiates two LocalDateTime
        LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10");
        LocalDateTime da_ti2 = LocalDateTime.now();

        // Display da_ti1, da_ti2
        System.out.println("LocalDateTime da_ti1 and da_ti2: ");
        System.out.println("da_ti1: " + da_ti1);
        System.out.println("da_ti2: " + da_ti2);

        System.out.println();

        // Here, this method compares two date-time
        // objects i.e. here it returns the 
        // difference of first non-matching field
        // in both the objects like first non-match
        // field is year da_ti1 = 2005 and da_ti2
        // = 2020 so the value (2005-2020 = -15)
        // -15 will be returned
        int compare = da_ti1.compareTo(da_ti2);

        // Display compare
        System.out.println("da_ti1.compareTo(da_ti2): " + compare);

        // Here, this method compares two date-time
        // objects i.e. here it returns the 
        // difference of first non-matching field
        // in both the objects like first non-match
        // field is year da_ti2 = 2020 and da_ti2
        // = 2005 so the value (2020 - 2005 = 15)
        // 15 will be returned
        compare = da_ti2.compareTo(da_ti1);

        // Display compare
        System.out.println("da_ti2.compareTo(da_ti1): " + compare);

        // Here, this method compares two date-time
        // objects i.e. here it returns 0 because
        // date-time are same in both the compared
        // objects
        compare = da_ti1.compareTo(da_ti1);

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

Output

LocalDateTime da_ti1 and da_ti2: 
da_ti1: 2005-10-05T10:10:10
da_ti2: 2020-06-04T08:35:27.674928

da_ti1.compareTo(da_ti2): -15
da_ti2.compareTo(da_ti1): 15
da_ti1.compareTo(da_ti1): 0


Comments and Discussions!

Load comments ↻





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