w3resource

Java Date.equals() Method

public boolean equals(Object obj)

The equals() method is used to compares two dates for equality.
The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.

Two Date objects will be equal if and only if the getTime method returns the same long value for both.

Package: java.util

Java Platform: Java SE 8

Syntax:

equals(Object obj)

Parameters:

Name Description
obj The object to compare with.

Return Value: true if the objects are the same; false otherwise.

Return Value Type: boolean

Pictorial Presentation of Jave Date equals() method:

Java Date.equals() Method

Example: Java Date.equals(Object obj) Method

import java.util.Date;
 
public class Main {
 
    public static void main(String[] args) {
 
        Date date1 = new Date(2018, 01, 20);
        Date date2 = new Date(2018, 01, 20);
        Date date3 = new Date(2018, 02, 20);
 
        boolean result = date1.equals(date2);
        System.out.println("If date1 and date2 are equal? " + result);
 
        result = date1.equals(date3);
        System.out.println("If date1 and date3 are equal? " + result);
     }
}

Output:

If date1 and date2 are equal? true
If date1 and date3 are equal? false

Java Code Editor:

Previous:compareTo Method
Next:from Method



Follow us on Facebook and Twitter for latest update.