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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 24-Apr-2019 11:57:43
Advertisements