Java LocalDateTime Class | equals() Method with Example

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

LocalDateTime Class equals() method

  • equals() method is available in java.time package.
  • equals() method is used to check whether this date-time and the given object are equal or not.
  • equals() 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.
  • equals() method does not throw an exception at the time of comparing two objects.

Syntax:

    public boolean equals(Object o);

Parameter(s):

  • Object o – represents the object is to be compared to this object.

Return value:

The return type of this method is boolean, it returns true when both the compared objects are equal otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean equals(Object o) method 
// of LocalDateTime

import java.time.*;

public class EqualsOfLocalDateTime {
    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 compare this date-time
        // to the given object for equality
        // i.e. it returns false because both
        // objects date and time are not same
        boolean status = da_ti1.equals(da_ti2);

        // Display status
        System.out.println("da_ti1.equals(da_ti2): " + status);

        // Here, this method compares this date-time
        // to the given object for equality
        // i.e. it returns true because both 
        // objects date and time are same
        status = da_ti1.equals(da_ti1);

        // Display status
        System.out.println("da_ti1.equals(da_ti1): " + status);
    }
}

Output

LocalDateTime da_ti1 and da_ti2: 
da_ti1: 2005-10-05T10:10:10
da_ti2: 2020-06-04T08:41:24.891685

da_ti1.equals(da_ti2): false
da_ti1.equals(da_ti1): true


Comments and Discussions!

Load comments ↻





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