2

I recently observed an oddity, when comparing two Java Date objects with equals(). Please note that both, this.getDate() and other.getDate(), will return a Java Date object in my application.

Code:

logger.debug("Date 1: " + this.getDate().toString());
logger.debug("Date 1: " + this.getDate().getTime());

logger.debug("Date 2: " + other.getDate().toString());
logger.debug("Date 2: " + other.getDate().getTime());

logger.debug("Dates are equal: " + this.getDate().equals(other.getDate()));
logger.debug("Dates match in comparison: " + (this.getDate().compareTo(other.getDate()) == 0));

Output: (added blank lines for better readability)

Date 1: 2014-07-28 00:00:00.0
Date 1: 1406498400000

Date 2: Mon Jul 28 00:00:00 CEST 2014
Date 2: 1406498400000

Dates are equal: false
Dates match in comparison: true

There are two things I don't get:

  1. Why does equals() return false?
  2. Why does the return value of toString() change its format?

I checked the documentation of Date.equals() where it says:

Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.

Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.

I also had a look at the implementation of Date.equals():

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

So why does equals() return false, even though getTime() returns 1406498400000 for both Date objects?

Reference: http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#equals(java.lang.Object)

7
  • Although I don't know why equals() returns false, you can always switch and use compareTo() to compare the Dates (a method I have yet to see fail!) Commented Jul 7, 2014 at 21:21
  • toString does not change the format, Date has no concept of format, it's just a container of the number of milliseconds. The toString ,ethos simple uses the current local to display a representation of the this value. Commented Jul 7, 2014 at 21:22
  • Can you update your example to include the construction of the Date() objects? Commented Jul 7, 2014 at 21:22
  • because .equals() compares objects and .compare() compares strings Commented Jul 7, 2014 at 21:24
  • 1
    Perhaps your getDate method is not doing what it would be expected to do. I think we should see it. Commented Jul 7, 2014 at 21:56

2 Answers 2

9

I guess they are not both Date objects as the formatting is different.

java.sql.Timestamp extends java.util.Date but it is a different class and is not equal.

From Timestamp

public boolean equals(java.lang.Object ts) {
  if (ts instanceof Timestamp) {
    return this.equals((Timestamp)ts);
  } else {
    return false;
  }
}

You might find that if you compare them the other way around it returns true. :P

try

logger.debug("Dates are equal: " + other.getDate().equals(this.getDate()));
Sign up to request clarification or add additional context in comments.

2 Comments

This is probably the reason. Seeing the docs of the toString() method of each class, this.getDate() probably returns a java.util.Date, while other.getDate() is returning java.sql.Date
Thanks Peter, you nailed it! Date 1 was instanceof java.sql.Timestamp (and instanceof java.util.Date). Date 2 was instanceof java.util.Date.
0

From the toString output

It is clear that Date1 is object of the class java.sql.Date and Date2 is object of java.util.Date

So the toString output is different and the equals() method is giving false.

Because (obj instanceof Date) will return false.

1 Comment

Ah, no, java.sql.Date is an instance of (instanceof) java.util.Date, java.sql.Date extends from java.util.Date. In fact, the only comparison that fails is Timestamp#equals(Date) because the object been matched isn't an instance of Timestamp...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.