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








Question

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

Answer

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
/*from ww  w.  j  av  a 2 s  .  co m*/
public class Main {

  public static void main(String[] args) {
    Instant t1 = Instant.now();
    long hours = 2;
    long minutes = 30;
    Instant t2 = t1.plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES);
    
    long minutesBetween = Duration.between(t1, t2).toMinutes();
    System.out.println(minutesBetween);
  }
}

The code above generates the following result.