-2

I want to convert print current time in nanoseconds.

I tried System.nanoTime() but it is giving like 275923649812830, If I try System.currentTimeMillis() it is giving like 1516915329788, for converting to nanoseconds I don't want to do System.currentTimeMillis()*1000000.

please help me with this and I'm looking for whole nanoseconds of wall-clock

and there are many questions about the nanoseconds but I did not find the solution to my problem

user9150771
  • 61
  • 1
  • 9
  • You need to clarify what you want and why System.nanoTime is unacceptable. It does not return System.currentTimeMillis()*1000000. Also, according to the docs, System.nanoTime does not return nano-precision time but nanosecond precision output of the local jvm clock, which is not necessarily the same thing. Are you looking for the actual time at nanosecond precision? that is, to put it mildly, "non-trivial". – Steve B. Jan 25 '18 at 21:32
  • Actually, I'm not looking for nano precision and I'm looking for whole nanoseconds – user9150771 Jan 26 '18 at 16:11
  • That is what nanosecond precision means: accurate to the nearest whole nanosecond. Even millisecond precision can be problematic ... if you are expecting the wall-clock to be synced with *real* time. – Stephen C Jan 29 '18 at 23:12

1 Answers1

2

There is no standard way to get the time in nanoseconds.

The javadoc says for System.nanoTime() says:

Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.

This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.

In reality, getting the current (wall-clock) time at that accuracy is impractical on most computers. A typical system uses NTP to synchronize the local wall-clock with an external (network based) source. Typically the the accuracy is "a few milliseconds". To synchronize with nanosecond precision would require a different technology; e.g. GPS ... or atomic clocks. (reference)

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I'm looking for the wall-clock nanoseconds – user9150771 Jan 26 '18 at 16:12
  • Well, you won't be able to get it in Java. And to be frank, it is **not meaningful** on most computers since the wall-clock is not synced to a time source accurately enough to give better than millisecond accuracy. I suggest that you think again about *why* you want nano clock, and come up with a more realistic *alternative* way of doing it. – Stephen C Jan 27 '18 at 00:13
  • In Couchbase the CAS value is generated based on nanoseconds and I want to compare the CAS value with current nanoseconds. – user9150771 Jan 29 '18 at 19:35
  • So compare it with whatever is used to generate it; e.g. if the CAS value is generated from the `System.getNano()` value, compare it against the current `System.getNano()` value. – Stephen C Jan 29 '18 at 22:17
  • Also, note that Couchbase has recognized that using the nano clock for CAS timestamps is a flaw and have fixed it: https://issues.couchbase.com/browse/MB-21617 – Stephen C Jan 29 '18 at 22:21
  • Error:(35, 43) java: cannot find symbol symbol: method getNano() location: class java.lang.System – user9150771 Jan 30 '18 at 16:59
  • I'm getting the above error when trying to run it – user9150771 Jan 30 '18 at 17:01