0

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?

2
  • 2
    Date#getTime returns a long and therefore does not have a equals method... Commented Feb 8, 2015 at 8:20
  • @MadProgrammer yes sorry fixed
    – abc
    Commented Feb 8, 2015 at 8:25

6 Answers 6

2

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.

5
  • "Comparison of Date objects compares the objects themselves, not the content" - Wrong - return obj instanceof Date && getTime() == ((Date) obj).getTime(); Commented Feb 8, 2015 at 8:21
  • You're assuming that if you call equals on a Date object, you get the equals implementation from Date. But you might get the implementation from a subclass which has a different way of determining equality other than just checking the getTime() field.
    – khelwood
    Commented Feb 8, 2015 at 8:36
  • OP are using Date in their example.
    – Maroun
    Commented Feb 8, 2015 at 8:40
  • @MarounMaroun No, they have two variables of type Date. They don't specify that they were instantiated as new Date. If you have two Date variables as function parameters, someone can call that function with two Timestamps.
    – khelwood
    Commented Feb 8, 2015 at 9:06
  • @khelwood you're right, I assumed that OP are asking for Date object, not a subclass of it.
    – Maroun
    Commented Feb 8, 2015 at 9:13
0

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.

1
  • But, if in the spirit of things, you were to say obj1.getTime() == obj2.getTime() instead, then there is no difference between the two statements Commented Feb 8, 2015 at 8:23
0

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();
0

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.

0
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.

0

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.

4
  • The OP has updated the question, so your first statement is wrong. You last statement is also wrong - return obj instanceof Date && getTime() == ((Date) obj).getTime(); - From the Date#equals method ;) Commented Feb 8, 2015 at 8:35
  • @MadProgrammer, what's wrong with the second statement? .equals is supposed to check for meaningful equality. In case of Date class, meaningful equality is defined as having the same long value representing them. When I say long representation is equal, I suppose it is implied that the classes must be of same type. Commented Feb 8, 2015 at 10:23
  • It sounds more like you are talking about the fact the equals is check to see if one object is equal to another (==) not that it's contents matches Commented Feb 8, 2015 at 10:24
  • Added a clarification. Commented Feb 8, 2015 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.