Java Utility Library

Java Date - equals() Method



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

Syntax

public boolean equals(Object obj)

Parameters

obj Specify the object to compare with.

Return Value

Returns true if the objects are the same; false otherwise.

Exception

NA.

Example:

In the example below, the java.util.Date.equals() method is used to compare given dates for equality.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Dates
    Date Dt1 = new Date();
    Date Dt2 = new Date(1000);
    Date Dt3 = new Date();

    //checking Dt1 and Dt2 for equality
    System.out.println("Does Dt1 equal to Dt2?: " + Dt1.equals(Dt2));

    //checking Dt1 and Dt3 for equality
    System.out.println("Does Dt1 equal to Dt3?: " + Dt1.equals(Dt3));
  }
}

The output of the above code will be:

Does Dt1 equal to Dt2?: false
Does Dt1 equal to Dt3?: true

❮ Java.util - Date