Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Date format SSSSSS: if not microseconds what are the last 3 digits?

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

like image 240
maxxyme Avatar asked Oct 07 '13 11:10

maxxyme


2 Answers

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)

like image 65
ppeterka Avatar answered Sep 24 '22 05:09

ppeterka


tl;dr

Instant.now()        .toString()  

2018-02-02T00:28:02.487114Z

Instant.parse(     "2018-02-02T00:28:02.487114Z" ) 

java.time

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" ) ) ; 

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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 36
Basil Bourque Avatar answered Sep 25 '22 05:09

Basil Bourque