3

I have used System.currentTimeInMillis() method for getting current timestamp in MilliSeconds. And also I used System.nanoTime(), but it is used for measuring elapsed time only.

How can I get Current Timestamp in nanoseconds accuracy?

Any help appreciated.

The Heist
  • 1,444
  • 1
  • 16
  • 32
  • I think I remember seeing a very similar question at some point, and if memory serves the conclusion was that you can't. A nanosecond is just too short and short of *very* specialized hardware there just isn't a reliable way to be that accurate. GPS is *very* accurate, and even for that you get >10 ns of variation. – awksp Jun 18 '14 at 09:22
  • You can't. But why do you need this? – AlexR Jun 18 '14 at 09:23
  • @AlexR I am executing bulk commit of update queries using Sqlite (through coding), and the primary key combination consist of current timestamp. At the time of execution, few record have same timestamp (that is in milliseconds). So i can't distinguish the records inserted. Any other possible solution for this? – The Heist Jun 18 '14 at 09:29
  • Sure, just save your previous timestamp and make sure that your next timestamp is at least one bigger than the last. – HHK Jun 18 '14 at 09:34
  • Don't use a timestamp as primary key. Use a *sequence* (either let the RDB generate the key automatically, or have a sequence generator in memory, e.g. AtomicInteger/-Long are well suited to that task). – Durandal Jun 18 '14 at 11:31

2 Answers2

1

@T-Rush, your answer to my comment IMHO should be added to your question. BTW this is the typical example of so called AB question.

The solution is not to use current timestamp as a primary key. Actually IMHO the preferable way is to use auto generated IDs. In this case this is the responsibility of DB.

If however for some reason you have to manage IDs on application level you have several possibilities as well.

  1. Use java.util.UUID. This guarantees uniqueness but does not allow you so sort your entries by ID that is sometimes useful.

  2. User combination of UTC timestamp retrieved from System.currentTimeMillis() when application starts and application time retrieved from System.nanoTime()

Obviously there are other solutions and I believe that if you give more details about your application and your data the community can suggest you better design.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 3
    Nitpick -- it's the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). But otherwise, good answer, as far as I can tell! – awksp Jun 18 '14 at 09:42
1

tl;dr

Instant.now() 

java.time

The modern approach uses the java.time classes built into Java 8 and later, and in the latest Android. For earlier Android, see the last bullets below.

Avoid the legacy date-time classes from the earliest versions of Java. They are troublesome, confusing, poorly-designed, and flawed.

Instant

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).

To capture the current moment in UTC, call Instant.now.

Instant instant = Instant.now() ;  // Current moment in UTC.

As of 2018, conventional computer hardware clocks are not capable of capturing the current moment with a resolution as fine as nanoseconds. Commonly, you will get the current moment only in microseconds or more coarse a granularity.

In the OpenJDK & Oracle implementations of Java, Java 8 captures the current moment only in milliseconds. Java 9 in those implementations brought a new implementation of Clock, now capable of capturing the current moment in a finer resolution. On macOS on a MacBook Pro (Retina, 15-inch, Late 2013) with the Oracle JDK using Java 9.0.1, I am seeing the current moment captured in six digits of decimal fraction, microseconds.

instant.toString(): 2018-01-09T05:26:04.327132Z


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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154