简体   繁体   English

如何将时间戳转换为time4j Moment格式?

[英]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. 我的Time4J库有问题,我无法将特定的timeStamp转换为Moment格式。

for example, I want something like this: Moment.timeInMillies = myTime or Moment.of(myTime) 例如,我想要这样的东西: Moment.timeInMillies = myTimeMoment.of(myTime)

myTime is a long value of milliseconds since the epoch, which can be obtained via System.currentTimeMillis() . myTime是自纪元以来的毫秒数的long整数,可以通过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 . MILLIS_SINCE_UNIX可以在LongMoment之间转换,因此调用会隐式将您的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 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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