How to get the duration between two Instant timestamps in Java

Java 8Object Oriented ProgrammingProgramming

Let us first set two Instants with ofEpochSeconds() method using seconds from the epoch of 1970-01- 01T00:00:00Z.

Now get the duration between the above two Instant:

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

Example

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

Output

PT8H23M27S
raja
Published on 24-Apr-2019 15:25:39
Advertisements