Your Question is quite confusing, not sure if you are capturing the current time in Java or in your database. Either way, you are using a troublesome old date-time class (java.sql.Timestamp
) that is now legacy, supplanted by java.time classes.
The java.time classes use a resolution of nanoseconds, for up to nine decimal digits of a fractional second. In any version of Java, you can carry a nanosecond value. However, capturing the current moment is a different story.
Java 8
Capturing the current moment in Java 8 is limited to milliseconds, for three digits of resolution.
Java 9 and later
Capturing the current moment in Java 9 uses a fresh implementation of java.time.Clock
with finer resolution, depending on the limits of your host computer hardware of course.
On macOS Sierra running on a MacBook Pro with Intel i7 chip using Oracle Java 9.0.4, I am capturing the current moment in microseconds for six digits of decimal fraction.
Instant.now()
2018-03-09T21:03:33.831515Z
With a JDBC driver supporting JDBC 4.2 and later, you should be able to directly exchange java.time objects with your database. No need to ever use java.sql.Timestamp
again.
If your DB2 column lacks time zone or offset-from-UTC info, use LocalDateTime
class in Java.
LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
If your database column is in UTC, use Instant
class.
Instant instant = myResultSet.getObject( … , Instant.class ) ;
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.