Java 程序以秒和纳秒为单位减去瞬间


让我们首先设置一个时间

Instant now = Instant.ofEpochMilli(184142540078l);

现在让我们从 Instant 中减去秒

Instant resSeconds = now.minusSeconds(50);

现在让我们从 Instant 中减去纳秒

Instant resNanoSeconds = now.minusNanos(10000);

示例

import java.time.Instant;
public class Demo {
   public static void main(String[] args) {
      Instant now = Instant.ofEpochMilli(184142540078l);
      System.out.println(now);
      Instant resSeconds = now.minusSeconds(50);
      System.out.println("After subtracting seconds = "+resSeconds);
      Instant resNanoSeconds = now.minusNanos(10000);
      System.out.println("After subtracting nanoseconds = "+resNanoSeconds);
   }
}

输出

1975-11-02T06:42:20.078Z
After subtracting seconds = 1975-11-02T06:41:30.078Z
After subtracting nanoseconds = 1975-11-02T06:42:20.077990Z

更新于: 30-Jul-2019

211 次浏览

开启您的 职业生涯

完成课程获得认证

开始
广告