Java Data Type How to - Get the seconds, Minutes between two Instant








Question

We would like to know how to get the seconds, Minutes between two Instant.

Answer

import java.time.Duration;
import java.time.Instant;
// w  w w  .j a v  a 2  s .c om
public class Main {
  public static void main(String[] args) {
    Instant firstInstant = Instant.ofEpochSecond(1294881180); 
    Instant secondInstant = Instant.ofEpochSecond(1294708260); 

    Duration between = Duration.between(firstInstant, secondInstant);

    System.out.println(between);
    
    long seconds = between.getSeconds();

    long absoluteResult = between.abs().toMinutes();

  }
}

The code above generates the following result.