abc
abc

Reputation: 15

java.util.Date equals() and getTime()

What is the difference between these two :

java.util.Date obj1;
java.util.Date obj2;

obj1.equals(obj2)
obj1.getTime() == (obj2.getTime())

Has it got any millisecond difference?

Upvotes: 0

Views: 1461

Answers (6)

Nibras Reeza
Nibras Reeza

Reputation: 127

Second one will cause a compiler error. :)

java.util.Date.getTime() returns the primitive long and not the object type Long. You cannot invoke methods on a primitive type. You are looking for, Long.compare(obj1.getTime(),obj2.getTime()) which will return 0 if both are equal. Alternatively, Long.valueOf(obj1.getTime()).equals(Long.valueOf(obj2.getTime()) can also be used. Both these convert long to Long before invoking the methods.

Secondly, in this particular case, both approaches will not result in different results. '.equals()' is meant to check if two objects are meaningfully equal to each other. As opposed to java.util.Date which is represented by a single long number, certain classes may have their equality defined by values of more than one property. For example, in a House class, equality will be defined by the aggregate of street address, city, zip code and country. In such a case, .equals() will be the only approach for equality.

Upvotes: 0

Ankur Piyush
Ankur Piyush

Reputation: 308

Date date1 = new Date();
Date date2 = new Date();

Case1: date2.getTime == date1.getTime This particular comparison, compares the number of milliseconds passes since 1970 till the instance of your both date objects. Data type will be primitive long.

Case2: date2.equals(date1) This particular comparison does the following,

public boolean equals(Object obj) { return obj instanceof Date && getTime() == ((Date) obj).getTime(); }

i.e case2 also compares the number of milliseconds passed since 1970 with an additional check of whether the comparable date is an instance of Date object.

Upvotes: 0

khelwood
khelwood

Reputation: 59146

The equals method defined in Date just checks if the getTime() returns the same for both object. So there really shouldn't be any different between using equals and checking the getTime() values yourself. If the libraries were all sensibly written, there wouldn't be.

However, at least one subclass of Date (java.sql.Timestamp) overrides equals and changes its meaning to be inconsistent with its baseclass. So if one of your Date objects happened to be a Timestamp, then its equals method could produce a different result.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

obj1.getTime().equals(obj2.getTime()) can't compile, asDate#getTimereturns a primitivelong` which does not have any methods.

If, other the other hand, you were to say obj1.getTime() == obj2.getTime(), then neither statement would be any different as the Date#equals method uses...

return obj instanceof Date && getTime() == ((Date) obj).getTime();

Upvotes: 0

Jens
Jens

Reputation: 69460

the first one compares to Dates. the second one does not compile, because long (is returned by getTime()) is a primitive type and you can not call a method on a primitive type.

Upvotes: 0

Maroun
Maroun

Reputation: 95978

See the implementation of Date#equals:

947     public boolean equals(Object obj) {
948         return obj instanceof Date && getTime() == ((Date) obj).getTime();
949     }

it compares using getTime method.

Date#getTime returns the "number of milliseconds since January 1, 1970, 00:00:00 GMT", you cannot use equals on it since it's a long, see its signature:

public long getTime()

So you should simply:

obj1.equals(obj2)

or using == operator to compare the value returned from getTime.

Upvotes: 2

Related Questions