How to get the duration between two Instant timestamps in Java


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

Updated on: 30-Jul-2019

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements