Java program that reads two times in military format and prints the number of hours and minutes between the two times
Posted by Samath
Last Updated: February 10, 2021
  1874

Write a java program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. Here is a sample run. User
input is in color.

Please enter the first time: 0900
Please enter the second time: 1730
8 hours 30 minutes

Extra credit if you can deal with the case where the first time is later than the second:

Please enter the first time: 1730
Please enter the second time: 0900
15 hours 30 minutes

Code:

import java.util.Scanner;

public class Main
{
        public static void main(String[] args) {
            
            final int MINUTES_IN_HOUR = 60;
            final int MINUTES_IN_DAY = 24 * 60;
            int firstTime;
            int firstTimeInMinutes;
            int secondTime;
            int secondTimeInMinutes;
            int minutes;
            int hours;
            
            Scanner in = new Scanner(System.in);
            
            System.out.print("Please enter the first time: ");
            firstTime = in.nextInt();
            firstTimeInMinutes = firstTime / 100 * MINUTES_IN_HOUR + firstTime % 100;
            
            System.out.print("Please enter the second time: ");
            secondTime = in.nextInt();
            in.close();
            
            secondTimeInMinutes = secondTime / 100 * MINUTES_IN_HOUR + secondTime % 100;
            minutes = secondTimeInMinutes - firstTimeInMinutes;
            minutes = (MINUTES_IN_DAY + minutes) % MINUTES_IN_DAY;
            hours = minutes / 60;
            minutes %= 60;
            
            System.out.printf("%d hours %d minutes", hours, minutes);
    }
}