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?
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
.
return obj instanceof Date && getTime() == ((Date) obj).getTime();
Commented
Feb 8, 2015 at 8:21
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.
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 Timestamp
s.
Date
object, not a subclass of it.
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.
obj1.getTime() == obj2.getTime()
instead, then there is no difference between the two statements
Commented
Feb 8, 2015 at 8:23
obj1.getTime().equals(obj2.getTime()) can't compile, as
Date#getTimereturns a primitive
long` 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();
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.
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.
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.
return obj instanceof Date && getTime() == ((Date) obj).getTime();
- From the Date#equals
method ;)
Commented
Feb 8, 2015 at 8:35
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
Date#getTime
returns along
and therefore does not have aequals
method...