Java Program to get minutes between two time instants

Java 8Object Oriented ProgrammingProgramming

At first, create two time instants −

Instant time1 = Instant.now();
Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(10, ChronoUnit.MINUTES);

Use between() to get the minutes between two time instants −

Duration duration = Duration.ofSeconds(13);
Instant i = time1.plus(duration);
System.out.println("Minutes between two time instants = "+Duration.between(time1, time2).toMinutes());

Example

 Live Demo

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class Demo {
   public static void main(String[] args) {
      Instant time1 = Instant.now();
      Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(10, ChronoUnit.MINUTES);
      Duration duration = Duration.ofSeconds(13);
      Instant i = time1.plus(duration);
      System.out.println("Minutes between two time instants = "+Duration.between(time1, time2).toMinutes());
   }
}

Output

Minutes between two time instants = 310
raja
Published on 24-Apr-2019 12:12:32
Advertisements