How can I convert timestamp to time4j Moment format?

157 Views Asked by At

I have a problem with Time4J library, I can not convert specific timeStamp to Moment format.

for example, I want something like this: Moment.timeInMillies = myTime or Moment.of(myTime)

myTime is a long value of milliseconds since the epoch, which can be obtained via System.currentTimeMillis().

1

There are 1 best solutions below

0
On BEST ANSWER

Edit: I think the best conversion is

TemporalType.MILLIS_SINCE_UNIX.translate(myTime)

MILLIS_SINCE_UNIX can translate between Long and Moment, so the call implicitly boxes your long.

Original answer: I haven’t got the experience, but the way I read the documentation either of the following should work:

  • Moment.of(myTime / 1000, Math.toIntExact(myTime % 1000 * 1_000_000), TimeScale.POSIX)
  • Moment.from(Instant.ofEpochMilli(myTime))

I might have missed a more elegant way, though, I don’t know.

Links