java.time
You are using the terribly troublesome old date-time classes (Date
, Calendar
) from the earliest versions of Java. These classes are now legacy, supplanted years ago by the java.time classes.
I'm not a Jackson user, so I do not know if Jackson has added built-in support for java.time yet. But I imagine it is likely.
If not, it looks like you can add a support module to Jackson for this purpose. See this article, this Question, and this GitHub site.
The java.time classes use nanosecond resolution. That means up to nine (9) decimal digits of fractional second. More than enough for the 6 digits in your inputs.
Zoned vs unzoned
You have another problem, of a different nature. You are taking input for a date-time lacking any concept of time zone or offset-from-UTC. Then you are saving it into a different type, a type with a time zone. Not good. You have injected value where none was deserved. This is like taking a generic number assumed to be a price, and then applying arbitrarily a particular currency not indicated in the input.
You should be parsing an input such as 2018-06-25T13:50:53.984771
as a LocalDateTime
. This type purposely lacks any time zone or offset-from-UTC, like your input. This type does not represent a specific moment, is not a point on the timeline, as it has no real meaning until you apply the context of a zone/offset.
For more discussion, search Stack Overflow for LocalDateTime
, ZonedDateTime
, OffsetDateTime
, and Instant
. In particular for some Answers of mine, search for "Santa".
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.