Just tested this code on both my Windows (8) workstation and an AIX:
public static void main(String[] args) { System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date())); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date())); }
and got something similar to this as a result:
2013-10-07 12:53:26.000905 2013-10-07 12:53:26.000906
Can someone please explain me what are the last digits, if not microseconds?
Note: I interact with a DB2 database in which chronological data is stored using timed columns as TIMESTAMP with 6 digits AFTER the seconds i.e. microseconds (IMO). But all those "timestamps" are created by requesting the following query:
SELECT current timestamp as currenttimestamp FROM Table ( values (1)) temp
I wonder if, given the above results, I couldn't just use in my code new Date()
instead of selecting the current timestamp
from the database.
Thanks.
PS: I searched but found no relevant (answered) questions, like: Current time in microseconds in java or Get time with hour, minute, second, millisecond, microsecond
From the documentation of SimpleDateFormat:
Letter Date or Time Component Presentation Examples S Millisecond Number 978
So it is milliseconds, or 1/1000th of a second. You just format it with on 6 digits, so you add 3 extra leading zeroes...
You can check it this way:
Date d =new Date(); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(d)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS").format(d)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(d)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS").format(d)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS").format(d)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(d));
Output:
2013-10-07 12:13:27.132 2013-10-07 12:13:27.132 2013-10-07 12:13:27.132 2013-10-07 12:13:27.0132 2013-10-07 12:13:27.00132 2013-10-07 12:13:27.000132
(Ideone fiddle)
Instant.now() .toString()
2018-02-02T00:28:02.487114Z
Instant.parse( "2018-02-02T00:28:02.487114Z" )
The accepted Answer by ppeterka is correct. Your abuse of the formatting pattern results in an erroneous display of data, while the internal value is always limited milliseconds.
The troublesome SimpleDateFormat
and Date
classes you are using are now legacy, supplanted by the java.time classes. The java.time classes handle nanoseconds resolution, much finer than the milliseconds limit of the legacy classes.
The equivalent to java.util.Date
is java.time.Instant
. You can even convert between them using new methods added to the old classes.
Instant instant = myJavaUtilDate.toInstant() ;
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Capture the current moment in UTC. Java 8 captures the current moment in milliseconds, while a new Clock
implementation in Java 9 captures the moment in finer granularity, typically microseconds though it depends on the capabilities of your computer hardware clock & OS & JVM implementation.
Instant instant = Instant.now() ;
Generate a String in standard ISO 8601 format.
String output = instant.toString() ;
2018-02-02T00:28:02.487114Z
To generate strings in other formats, search Stack Overflow for DateTimeFormatter
, already covered many times.
To adjust into a time zone other than UTC, use ZonedDateTime
.
ZonedDateTime zdt = instant.atZone( ZoneId.of( "Pacific/Auckland" ) ) ;
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With