When I test this code:
java.util.Date date = new java.util.Date();
java.util.Date stamp = new java.sql.Timestamp(date.getTime());
assertTrue(date.equals(sta
date.equals(stamp) return true AND stamp equals(date) returns false. REASON :Date neglects the nanosecond part of timestamp and since the other parts happen to be equal so the result is equal. The fractional seconds - the nanos - are separate.The Timestamp.equals(Object) method never returns true when passed a value of type java.util.Date because the nanos component of a date is unknown. See here for more details
Timestamp
's nanos value is NOT the number of nanoseconds - it's a nanosecond-resolution number of millis (i.e. fractional seconds). As such, in the Timestamp
constructor, it is setting the time on the super to be without milliseconds. Therefore, the Timestamp
will always have a lower value for the member fastTime
(used in Date's compareTo()
) than the corresponding Date
(unless, of course, it has no fractional seconds).
Check the source for Timestamp at line 110.
A Small note on Implementaion inheritance and Type inheritance..
"An object's class defines how the object is implemented. In contrast, an object's type only refers to its interface. Class inheritance(Implementation inheritance) defines an object's implementation in terms of another object's implementation. Type inheritance describes when an object can be used in place of another."
Timestamp and Date classes have implementation inheritance as JAVADOC said.
I've resolved converting both Date and TimeStamp into a Calendar object and then i've compared the single Calendar's properties:
Calendar date = Calendar.getInstance();
date.setTimeInMillis(dateObject.getTime());
Calendar timestamp = Calendar.getInstance();
timestamp.setTimeInMillis(timestampObject.getTime());
if (effettoAppendice.get(Calendar.DAY_OF_MONTH) == timestamp.get(Calendar.DAY_OF_MONTH) &&
effettoAppendice.get(Calendar.MONTH) == timestamp.get(Calendar.MONTH) &&
effettoAppendice.get(Calendar.YEAR) == timestamp.get(Calendar.YEAR)) {
System.out.println("Date and Timestamp are the same");
} else {
System.out.println("Date and Timestamp are NOT the same");
}
Hope this helps.
Take a look at the source code compare method for Timestamp:
public boolean equals(java.lang.Object ts) {
if (ts instanceof Timestamp) {
return this.equals((Timestamp)ts);
} else {
return false;
}
}
http://www.docjar.com/html/api/java/sql/Timestamp.java.html
It will only ever return true if the comparing object is a timestamp. Also, here is the Date source code: http://www.docjar.com/html/api/java/util/Date.java.html , and since Timestamp inherits Date, it can compare it.
Nican explained the equals
part, about compareTo
:
Timestamp
has a compareTo(Date)
method that converts it to Timestamp
internally Date
does the comparison by downcasting (since Timestamp
is a subclass of it); but as the javadoc states: "The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance"Which of course is an horrible idea, in my opinion.