I just encountered a really strange behaviour. I wanted to compare 2 dates which were exactly the same using d1.equals(d2);
, but the result was false.
While debugging I found that both dates had the "fast time" of 1531951200000
. Afterwards I change the check to d1.getTime() == d2.getTime()
and got the result that both are equal.
I also checked the equals()
method of java.util.Date
which does pretty much exactly the same
public boolean equals(Object obj) {
return obj instanceof Date && getTime() == ((Date) obj).getTime();
}
Please note that both objects were of type java.util.Date
which rules out that there might have been a difference, i.e. java.util.Date
vs java.sql.Date
. Also note that one of the values was read from database while the other one was read from a file. Both values were set to a different instance of the same class with a java.util.Date
field.
Has anyone an explanation for that? Or am I just missing something?