时代时间戳记,找到天数

时间:2019-05-21 12:32:16

标签: java

我正在尝试从以下时期的时间戳中查找从今天起的天数:-

  

1560593315387

像这样:

System.out.println(ChronoUnit.DAYS.between(Instant.ofEpochSecond(1558353632),Instant.now()));

对于1558353632来说,它可以正常工作,但对于1560593315387,它无法转换并且没有给出预期的结果。

2 个答案:

答案 0 :(得分:2)

1560593315387看起来是毫秒,而不是秒,因此请使用Instant.ofEpochMilli

它也太长(哈!),无法放入int,因此您必须使用long文字(with an L at the end)。

 ChronoUnit.DAYS.between(Instant.ofEpochMilli(1560593315387L), Instant.now())

答案 1 :(得分:0)

请这样做

    int seconds = (int) 1560593315387l / 1000;// (millisecond to senconds conversion)

    System.out.println(ChronoUnit.DAYS.between(Instant.ofEpochSecond(seconds), Instant.now()));