Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the new Java 8 Date Time API not have nanosecond precision? [duplicate]

One of the features of the new Date Time API in Java 8 is supposed to be nanosecond precision. However when I print the current Date Time to the console like so

DateTimeFormatter formatter = DateTimeFormatter     .ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ"); System.out.println(OffsetDateTime.now().format(formatter));  

I only see millisecond precision: 2015-11-02T12:33:26,746000000+0100

The operating system does seem to support nanosecond precision. When I print the current date time via the Terminal

date -Ins 

I see 2015-11-02T12:33:26,746134417+0100

How do I get nanosecond precision in Java? I'm running Oracle Java 1.8.0_66 on Ubuntu 14.04 64-bit

like image 585
Thomas Oellrich Avatar asked Nov 02 '15 12:11

Thomas Oellrich


People also ask

What is the feature of the new date and time API in Java 8?

The new date-time API is immutable and does not have setter methods. Poor design − Default Date starts from 1900, month starts from 1, and day starts from 0, so no uniformity. The old API had less direct methods for date operations. The new API provides numerous utility methods for such operations.

Why date API in Java 8?

Java 8 provides ZonedDateTime when we need to deal with time-zone-specific date and time. The ZoneId is an identifier used to represent different zones. There are about 40 different time zones, and the ZoneId represents them as follows.

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

Is Java util date thread-safe?

Not thread safe − java. util. Date is not thread safe, thus developers have to deal with concurrency issue while using date. The new date-time API is immutable and does not have setter methods.


1 Answers

The java.time API in general does have nanosecond precision. For example:

DateTimeFormatter formatter = DateTimeFormatter     .ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ"); OffsetDateTime odt = OffsetDateTime.of(2015, 11, 2, 12, 38, 0, 123456789, ZoneOffset.UTC); System.out.println(odt.format(formatter)); 

Output:

2015-11-02T12:38:00,123456789+0000 

However, it's the clock value returned by OffsetDateTime.now() which is returning a value which only has milliseconds.

From Clock implementation in Java 8:

The clock implementation provided here is based on System.currentTimeMillis(). That method provides little to no guarantee about the accuracy of the clock. Applications requiring a more accurate clock must implement this abstract class themselves using a different external clock, such as an NTP server.

So there's nothing inherently imprecise here - just the default implementation of Clock using System.currentTimeMillis(). You could potentially create your own more precise subclass. However, you should note that adding more precision without adding more accuracy probably isn't terribly useful. (There are times when it might be, admittedly...)

like image 101
Jon Skeet Avatar answered Oct 02 '22 15:10

Jon Skeet