How can I convert timestamp to time4j Moment format?

Amir Hossein Ghasemi :

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

Ole V.V. :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144902&siteId=1