Java Data Type How to - Get duration between two time instants


We would like to know how to get duration between two time instants.


import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
/*from w ww. jav  a  2 s  .c om*/
public class Main {

  public static void main(String[] args) {
    Instant t1 =;
    Instant t2 =;
    long nanos = Duration.between(t1, t2).toNanos();

    Duration gap = Duration.ofSeconds(13); // 12000000000
    Instant later =;
    System.out.println(t1); // 2014-07-02T19:55:00.956Z
    System.out.println(later); // 2014-07-02T19:55:13.956Z

    System.out.println(ChronoUnit.MILLIS.between(t1, t2)); // 12000

The code above generates the following result.