Date equals() method in Java with Examples

02 Jan, 2019
The equals() method of Java Date class checks if two Dates are equal, based on millisecond difference.
Syntax:
public boolean equals(Object obj)

Parameters: The function accepts a single parameter obj which specifies the object to be compared with.
Return Value: The function gives 2 return values specified below:
true if the objects are equal.
false if the objects are not equal.
Exception: The function does not throws any exception.
Program below demonstrates the above mentioned function:
// Java code to demonstrate// equals() function of Date class  import java.util.Date;import java.util.Calendar;public class GfG {    // main method    public static void main(String[] args)    {          // creating a Calendar object        Calendar c = Calendar.getInstance();          // set Month        // MONTH starts with 0 i.e. ( 0 - Jan)        c.set(Calendar.MONTH, 11);          // set Date        c.set(Calendar.DATE, 05);          // set Year        c.set(Calendar.YEAR, 1996);          // creating a date object with specified time.        Date dateOne = c.getTime();          System.out.println("Date 1: " + dateOne);          // creating a date of object        // storing the current date        Date currentDate = new Date();          System.out.println("Date 2: " + currentDate);          System.out.println("Are both dates equal: "                           + currentDate.equals(dateOne));    }}
Date 1: Thu Dec 05 08:19:56 UTC 1996
Date 2: Wed Jan 02 08:19:56 UTC 2019
Are both dates equal: false

// Java code to demonstrate// equals() function of Date class  import java.util.Date;import java.util.Calendar;public class GfG {    // main method    public static void main(String[] args)    {          // creating a Calendar object        Calendar c1 = Calendar.getInstance();          // set Month        // MONTH starts with 0 i.e. ( 0 - Jan)        c1.set(Calendar.MONTH, 11);          // set Date        c1.set(Calendar.DATE, 05);          // set Year        c1.set(Calendar.YEAR, 1996);          // creating a date object with specified time.        Date dateOne = c1.getTime();          System.out.println("Date 1: " + dateOne);          // creating a Calendar object        Calendar c2 = Calendar.getInstance();          // set Month        // MONTH starts with 0 i.e. ( 0 - Jan)        c2.set(Calendar.MONTH, 11);          // set Date        c2.set(Calendar.DATE, 05);          // set Year        c2.set(Calendar.YEAR, 1995);          // creating a date object with specified time.        Date dateTwo = c2.getTime();          System.out.println("Date 1: " + dateTwo);          System.out.println("Are both dates equal: "                           + dateTwo.equals(dateOne));    }}
Date 1: Thu Dec 05 08:20:05 UTC 1996
Date 1: Tue Dec 05 08:20:05 UTC 1995
Are both dates equal: false

Java - util package
Java-Functions
Java-util-Date
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java