Java Program to minus seconds and nanoseconds from Instant

Java 8Object Oriented ProgrammingProgramming

Let us first set an Instant:

Instant now = Instant.ofEpochMilli(184142540078l);

Let us now minus seconds from Instant:

Instant resSeconds = now.minusSeconds(50);

Let us now minus nanoseconds from Instant:

Instant resNanoSeconds = now.minusNanos(10000);

Example

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);
   }
}

Output

1975-11-02T06:42:20.078Z
After subtracting seconds = 1975-11-02T06:41:30.078Z
After subtracting nanoseconds = 1975-11-02T06:42:20.077990Z
raja
Published on 24-Apr-2019 15:29:33
Advertisements