-1

I have a unix time since epoc in milisecond e.g.: 1505913038295

I need to slice this into two things. So I can use with the Java Instant class method.

Instant ofEpochSecond(long epochSecond, long nanoAdjustment) 
  1. Seconds since epoch (This I can do) long secondsSince1970 = milisecondsSince1970 / 1000;

  2. This I need help. I need to take the miliseconds remainder, say 295, (1st Question: How do I get the remainder) then convert that to nanoseconds, then get the current System.nanoTime() and add that to the nano ramainder.

Wed Sep 20 2017 13:21:33 + [ remainder mili in nano + nono now] [seconds since epoc] + [miliseconds since epoc in nano + nono ]

Then hopefully I can call Instant.ofEpochSecond, something like this.

Instant convertedTimestamp = Instant.ofEpochSecond(secondsSince1970, remaindingNanoTime % 1000000000L);

I hope this makes sence.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mrmannione
  • 749
  • 10
  • 29
  • 1
    What for? What are you trying to do with this? You might also want to read the documentation for `System.nanoTime()`. – Kayaman Sep 20 '17 at 13:35
  • 2
    If I understood correctly, what you need is `Instant.ofEpochMilli(1505913038295L).plusNanos(System.nanoTime())`. Is that? –  Sep 20 '17 at 13:39
  • Try this: `Instant.ofEpochSecond(millis / 1000, millis % 1000 * 1000_000L);` – Usagi Miyamoto Sep 20 '17 at 13:39
  • @Kayaman & @Hugo : What does `System.nanoTime()` has to do with a given timestamp in millisceonds? – Usagi Miyamoto Sep 20 '17 at 13:41
  • 1
    @UsagiMiyamoto The questions says that `System.nanoTime()` must be added to the nanos value. Not sure why the OP wants to do that, but that's what I understood –  Sep 20 '17 at 13:43
  • 3
    This sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), What are you really trying to achieve? I'm asking, because `System.nanoTime()` is *not* the nano part of the current time. If you're trying to get the current time to nanosecond precision, then you need a `Clock` with that precision, and that's a totally different question. Or is it that you focused on the wrong method, trying to use `ofEpochSecond()`, when you really needed to use `ofEpochMilli()`? – Andreas Sep 20 '17 at 13:45
  • This is nearly what I want: Instant.ofEpochMilli(1505913038295L).plusNanos(System.nanoTi‌​me()) but instead of plusNanos(System.nanoTi‌​me()) i need something like plusNanos(current nano second or random nanosecods) – mrmannione Sep 20 '17 at 13:58
  • To get random values, use a [`java.util.Random`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html) –  Sep 20 '17 at 14:06
  • How can I get the current nanosecond, in the scale 1 - 999999 (representing 1 microsecond) ? – mrmannione Sep 20 '17 at 14:15
  • @mrmannione I don't get what you mean by *"scale 1 - 999999 (representing 1 millisecond)"* because 999999 nanoseconds are **less** than 1 millisecond. `Instant.now().getNano()` will return the nanoseconds part of the current date, is that what you need? It's not clear what you're trying to achieve. If you could please [edit] the question and make it clear about what you're trying to do, what should be the output and so on, maybe someone can come up with an answer (much better than trying to guess in the comments) –  Sep 20 '17 at 14:25

1 Answers1

0

If i understand correctly what you want, try something like this:

Instant.ofEpochMilli( millis ).plusNanos( System.nanoTi‌​‌​me() % 1000_000L);
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
  • 1
    Be careful with this. System.nanoTime() is usually not in sync with the system clock which is used for System.currentTimeMillis()! There is no portable way of accessing a clock with nano precision. System.nanoTime can be used to calculate durations, but cannot be safely interpreted as wall-clock time! – Florian Fray Sep 25 '17 at 08:06