How to get the seconds and minutes between two Instant timestamps in Java


The following are the two Instant timestamps:

Instant one = Instant.ofEpochSecond(1355836728);
Instant two = Instant.ofEpochSecond(1355866935);

Get the Duration between both the Instant:

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

Now, get the seconds between the two timestamps:

long seconds = res.getSeconds();

Now, get the minutes between the two timestamps:

long minutes = res.abs().toMinutes();

Example

import java.time.Duration;
import java.time.Instant;
public class Demo {
   public static void main(String[] args) {
      Instant one = Instant.ofEpochSecond(1355836728);
      Instant two = Instant.ofEpochSecond(1355866935);
      Duration res = Duration.between(one, two);
      System.out.println(res);
      long seconds = res.getSeconds();
      System.out.println("Seconds between Durations = "+seconds);
      long minutes = res.abs().toMinutes();
      System.out.println("Minutes between Durations = "+minutes);
   }
}

Output

PT8H23M27S
Seconds between Durations = 30207
Minutes between Durations = 503

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements