Is the method java.util.Date
method .equals
indifferent in what concerns the date format
or not? I mean I've tried the date.before
and date.after
with different formats and they behaved normally, however, i am unsure about the Date.equals
as I fear it behaves as the java.lang.Object.equals
, I am seeking a firm and definitive answer.
Is the method java.util.Date method .equals indifferent in what concerns the date format or not?
A Date
object doesn't have a format - it has no notion of where the information came from. It is just a number of milliseconds since the Unix epoch (January 1st 1970, midnight UTC). It also doesn't know about time zones. So you could have two Date
objects created in very different ways which end up representing the same point in time - and equals
would return true
there.
It's really important to understand what information is and isn't part of the state of an object - particularly for date and time APIs, where it can be counterintuitive in some places. When in any doubt, read the documentation carefully. For example, from the documentation for Date
:
The class Date represents a specific instant in time, with millisecond precision.
and in equals
:
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 thegetTime
method returns the same long value for both.
See more on this question at Stackoverflow