简体   繁体   中英

How can I convert timestamp to time4j Moment format?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM