如何用 Java 获取两个 Instant 时间戳之间的持续时间


我们首先使用 ofEpochSeconds() 方法设置两个 Instant,方法使用 1970-01- 01T00:00:00Z 纪元的秒数。

现在获取上述两个 Instant 之间的持续时间

Duration res = Duration.between(one, two);

实例

import java.time.Duration;
import java.time.Instant;
public class Demo {
   public static void main(String[] args) {
      Instant one = Instant.ofEpochSecond(1845836728);
      Instant two = Instant.ofEpochSecond(1845866935);
      Duration res = Duration.between(one, two);
      System.out.println(res);
   }
}

输出

PT8H23M27S

更新日期:30-Jul-2019

19K+ 浏览次数

开启您的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.