Date equals() method in Java with Examples Last Updated : 02 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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)); } } Output: 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 // 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)); } } Output: 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 Comment More infoAdvertise with us Next Article Date setTime() method in Java with Examples T Twinkl Bajaj Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-util-Date +1 More Practice Tags : JavaMisc Similar Reads Date from() method in Java with Examples The from(Instant inst) method of Java Date class returns an instance of date which is obtained from an Instant object. Syntax: public static Date from(Instant inst) Parameters: The method takes one parameter inst of Instant type which is required to be converted. Return Value: The method returns a d 2 min read Date before() method in Java with Examples The before() method of Java Date class tests whether the date is before the specified date or not. Syntax: public boolean before(Date when) Parameters: The function accepts a single parameter when which specifies the date that has to be checked. Return Value: It returns a Boolean value. It will retu 2 min read Calendar add() Method in Java with Examples The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar's rules. Syntax: public abstract void add(int field, int amt) Parameters: The method takes two parameters: The field of the 3 min read Date getTime() method in Java with Examples The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object. Syntax: public long getTime() Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 2 min read Date setTime() method in Java with Examples The setTime() method of Java Date class sets a date object. It sets date object to represent time milliseconds after January 1, 1970 00:00:00 GMT. Syntax: public void setTime(long time) Parameters: The function accepts a single parameter time which specifies the number of milliseconds. Return Value: 2 min read Date toString() method in Java with Examples The toString() method of Java Date class converts this date object into a String in form "dow mon dd hh:mm:ss zzz yyy". This method overrides toString in class object. Syntax: public String toString() Parameters: The function does not accept any parameter. Return Value: It method returns a string re 2 min read Like