6

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?

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • 3
    Can you post the exact code please? [mcve] :P – Lino Jul 20 '18 at 08:20
  • 2
    try getTime().equals(((Date) obj).getTime()) – Antoniossss Jul 20 '18 at 08:21
  • 1
    can you show us the format of both dates please? and how you get them from database and from file? – Youcef LAIDANI Jul 20 '18 at 08:21
  • 8
    Are you *sure* both objects were really of type `java.util.Date`? If you just did an instanceof check on them and this returned true, that doesn't tell you what you need to know (since the `sql` dates / timestamps are subclasses.) This is my best guess - in particular the `Timestamp` class has some cases where its `equals()` method is asymmetric, which is a common trap. – Michael Berry Jul 20 '18 at 08:22
  • check if `d1.getClass() == d2.getClass()` – Lino Jul 20 '18 at 08:22
  • Or try Long.compare(x,y)==0; – Antoniossss Jul 20 '18 at 08:23
  • there is not enough information (code) - without a [mcve] it will be very difficult for us to guess what you have done or what is missing.... – user85421 Jul 20 '18 at 08:26
  • 5
    Alright, nevermind, I just checked the classes and one of the classes was in fact `java.sql.Timestamp`. Looks like a mistake on my side for not checking that. I just assumed since both are cast to `java.util.Date` that there shouldn't be any problem. [This question](https://stackoverflow.com/questions/27661640/java-util-date-equals-doesnt-seem-to-work-as-expected) is basically the duplicate – XtremeBaumer Jul 20 '18 at 08:26
  • 3
    Ah, I love it when I guess correctly ;) Remember that [casting doesn't magically change the object's type](https://stackoverflow.com/questions/4862960/explicit-casting-from-super-class-to-subclass/4863090#4863090). – Michael Berry Jul 20 '18 at 08:27
  • Don't worry bro, yesterday I spent 20 minutes, because I was comparing HashMultimap to ImmutableListMultimap and had absolutely no idea. – Shadov Jul 20 '18 at 08:29
  • @MichaelBerry yeah I should have just checked the hibernate annotations for that field... `@Temporal(TemporalType.TIMESTAMP)` explains it all – XtremeBaumer Jul 20 '18 at 08:30
  • @XtremeBaumer No problem. I've fallen into a similar trap in the past, hence that's what came to mind. – Michael Berry Jul 20 '18 at 08:36
  • Why in 2018 are you still using the outdated `Date` and `Timestamp` classes? [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. And doesn’t contain the funny (OOP-wise incorrect) inheritance relationships that were a great part of the reason for your confusion. – Ole V.V. Jul 20 '18 at 10:51
  • 1
    @OleV.V. old codebase that was just recently even allowed to compile java 8. Right now too much work to refactor all the code to use java 8 features – XtremeBaumer Jul 20 '18 at 11:05
  • 1
    That I understand. If compiling with Java 8 an easy comparison of point in time that would work across `Date` and `Timestamp` would be dateOrTimestamp1.toInstant().equals(dateOrTimestamp2.toInstant()` (contrary to comparing `getTime()` it will get the comparison of fraction of second correct too). – Ole V.V. Jul 20 '18 at 13:07

0 Answers0