Reputation: 185
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.
Upvotes: 1
Views: 3560
Reputation: 16209
A java.util.Date is simply a wrapper around a long value representing the number of milliseconds since epoch (1970-01-01 00:00:00.00). The equals method just compares these values.
See the API:
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.
And the source code (of OpenJDK):
947 public boolean More ...equals(Object obj) {
948 return obj instanceof Date && getTime() == ((Date) obj).getTime();
949 }
Upvotes: 1
Reputation: 1502006
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.
Upvotes: 5
Reputation: 181
i Guess this will help.
Equals method in date 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.
Overrides: equals(...) in Object Parameters: obj the object to compare with. Returns: true if the objects are the same; false otherwise. See Also: java.util.Date.getTime()
Upvotes: 0
Reputation: 36304
You should really look at the Date Class documentation. It states :
public boolean equals(Object obj) 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.
So, it doesn't behave in the same way the default equals in Object
class behaves.
with different formats and they behaved normally : Since date is being converted to absolute time, the date format will not make any difference.
Upvotes: 2