1

I know this can be done in Linux, through a native call to clock_gettime from sys/time.h.

I know this can be done with System.currentTimeMillis() * 1000000L, which is not good because you can have two equal epoch times if you call that in succession.

Wondering if in the more recent versions of Java there is already a Java way of getting Epoch in nanoseconds.

NOTE: I want the epoch with nanosecond precision. I just don't want to convert it to milliseconds with something like:

long nanos = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());

Thanks for any help!


EDIT1: I'm not worried about getting the same epoch time twice. My goal is to have a real epoch time in nanoseconds.

  • Just in case you want to depend on the fact that two calls return different values: Don't. Even if a system can provide higher-than-millisecond resolution, there's absolutely no guarantee that two calls to whichever method provides this return different values. For example it's quite possible that a OS can return values with 1/10th millisecond resolution but not true nanosecond resolution. – Joachim Sauer Apr 24 '23 at 13:29
  • @JoachimSauer Thanks for the warning. My problem is actually precision, i need the nanosecond information. Returning the same time is not a problem. I should have been more clear about that. –  Apr 24 '23 at 13:43
  • I'm shocked that this question hasn't been closed and/or downgraded. Has anything changed in SO recently? Was it liberated from the [tyrannical mods](https://www.youtube.com/watch?v=IbDAmvUwo5c)? What happened to: _It has already been answered!_ _We don't do your homework!_ _Don't you know how to Google?_ –  Apr 24 '23 at 14:03
  • 3
    I'm not sure what you're trying to achieve with this comment. Are you complaining that you're not being treated unfairly enough? – Joachim Sauer Apr 24 '23 at 14:33
  • To satiate your desire for perceived unfair interactions, I've closed this question as a duplicate with some very good answers that tell you both ways to achieve that and caveats to keep in mind (note: I didn't really do that for the lolz, I did it because the dupe actually fully answers what this is asking). – Joachim Sauer Apr 24 '23 at 14:37
  • @JavaCoderUkraine Why the poor attitude? Your question was up voted. You were provided an answer. Then you complain for not having your question closed. Then you complain when it *is* closed. – WJS Apr 25 '23 at 15:09
  • Sorry but SO is irrelevant. It is only good as a read-only reference. For anything new we go to Reddit. –  Apr 28 '23 at 00:35

1 Answers1

3

This should work. Instant and other recent time improvements are from the java.time package.

Instant t = Instant.now();
System.out.println(t.getEpochSecond()*1_000_000_000 + t.getNano());

prints something like

1682327653903147800

Of course, by the time instant.now() has returned, the time has certainly changed significantly from a nanosecond perspective.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    Just to be clear to everyone: `System.nanoTime()` is **not** epoch –  Apr 24 '23 at 13:41
  • Sorry - my bad on the time – g00se Apr 24 '23 at 13:43
  • To elaborate on why `System.nanoTime` is not epoch: it returns the elapsed time in nanoseconds since an *arbitrary* instant (the *origin* time). [See the JavaDocs](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/System.html#nanoTime()) for more details. – MC Emperor Apr 24 '23 at 13:47