tl;dr
originalInstant.equals(
org.threeten.bp.DateTimeUtils.toInstant( mySqlTimestamp )
)
Avoid legacy date-time classes
The old date-time classes bundled with the earliest versions of Java are an awful mess, with poor designs and awkward hacks. One of those bad hacks is making java.sql.Timestamp
a subclass of java.util.Date
while telling you to ignore that fact of inheritance.
To quote the class doc (emphasis mine):
Due to the differences between the Timestamp class and the java.util.Date class mentioned above, it is recommended that code not view Timestamp values generically as an instance of java.util.Date. The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.
You were told to pretend they are not related classes. So your attempt to compare objects of each type is inappropriate.
Data loss
The Timestamp
has a resolution up to nanoseconds. The java.util.Date
class is limited to milliseconds. So the two will not compare as equal.
Using java.time
Instead, use the java.time classes. Much of their functionality is available as a back-port to Java 6 – see below.
When you get your Timestamp
, immediately convert to the java.time types. In Java 8 and later you could call the new methods added to those old classes for conversion. In the ThreeTen-Backport library for Java 6 & 7, use org.threeten.bp.DateTimeUtils.toInstant( Timestamp )
An Instant
is a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = DateTimeUtils.toInstant( mySqlTimestamp ) ;
Now compare to your original Instant
.
boolean isOriginal = originalInstant.equals( instant ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?