Home »
Java programming language
Java Date equals() Method with Example
Date Class equals() method: Here, we are going to learn about the equals() method of Date Class with its syntax and example.
Submitted by Preeti Jain, on February 09, 2020
Date Class equals() method
- equals() method is available in java.util package.
- equals() method is used to check whether this date and the given object (o) are equals 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 checking equality of this date object with the given object (o).
Syntax:
public boolean equals(Object o);
Parameter(s):
- Object o – represents the object to be compared with this date.
Return value:
The return type of this method is boolean, it returns true when two date objects are equals otherwise it returns false.
Example:
// Java program to demonstrate the example
// of boolean equals(Object o) method of Date
import java.util.*;
public class EqualsOfDate {
public static void main(String[] args) {
// create two Date object with two dates
Date this_date = new Date(2016, 8, 20);
Date given_date = new Date(2010, 11, 30);
// By using equals() method is to check
// equality of this_date and the given_date
boolean status = this_date.equals(given_date);
// Display status
System.out.println("this_date.equals(given_date): " + status);
}
}
Output
this_date.equals(given_date): false