Java Program to get minutes between two time instants


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

Updated on: 30-Jul-2019

414 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements