tl;dr
Use modern java.time classes, never terrible legacy date-time classes.
Instant
.ofEpochMilli( 1_532_654_534_390L )
.isAfter(
LocalDateTime
.parse(
"2018-07-27 01:22:14.077"
.replace( " " , "T" )
)
.atOffset(
ZoneOffset.UTC
)
.toInstant()
)
Doc says: Don’t use Timestamp
object as Date
Your code:
Date date2 = new Timestamp… // Violates class documentation.
…violates the contract established in the class documentation.
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.
The doc notes that while java.sql.Timestamp
technically inherits from java.util.Date
, you are instructed to ignore that fact of inheritance. You are not to use Timestamp
object as a Date
. Your code is doing exactly what the doc told you not to do.
Of course this pretend-it-is-not-a-subclass policy is a ridiculously bad class design. This hack is one of many reasons to never use these classes.
The behavior you saw, regarding mismatch of milliseconds fractional-second and nanoseconds, is documented:
Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. The fractional seconds - the nanos - are separate. The Timestamp.equals(Object) method never returns true when passed an object that isn't an instance of java.sql.Timestamp, because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method. Also, the hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation.
java.time
You are using notoriously terrible classes. The problem you found is due to their awful design that used bad hacks. Do not bother trying to understand these classes; just avoid them entirely.
These legacy classes were supplanted years ago by the java.time classes.
Parse your input string.
LocalDateTime ldt = LocalDateTime.parse( "2018-07-27 01:22:14.077".replace( " " , "T" ) ; // Without a time zone or offset, this value has no specific meaning, is *not* a point on the timeline.
Apparently you know for a fact that input string was implicitly representing a moment in UTC.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ; // Assign an offset-from-UTC to give the date and time a meaning as an actual point on the timeline.
Parse the other input, apparently a count of milliseconds since the first moment of 1970 in UTC. The Instant
class is more basic than OffsetDateTime
, a moment in UTC, always UTC by definition.
Instant instant = Instant.ofEpochMilli( 1_532_654_534_390L ) ; // Translate a count of milliseconds from 1970-01-01T00:00:00Z into a moment on the timeline in UTC.
Compare.
Boolean stringIsAfterLong = odt.toInstant().isAfter( 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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Timestamp
as aDate
, predicts the behavior you saw, and explains why. No news here.date2
as ajava.util.Date
and Hibernate returns me ajava.sql.Timestamp
. I haven't noticed that for 6-8 years I'm working on this application until now.Date
/Calendar
/Timestamp
.