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


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


import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
/*from   www.  jav a2 s  .c o  m*/
public class Main {

  public static void main(String[] args) {
    Instant t1 =;
    long hours = 2;
    long minutes = 30;
    Instant t2 =, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES);
    Duration gap = Duration.ofSeconds(13);
    Instant later =;
    System.out.println(ChronoUnit.MILLIS.between(t1, t2));  

The code above generates the following result.