Java program that read two military times and determine which comes first
Posted by Samath
Last Updated: May 08, 2021
  163

Write a Java program that reads two military times and determines which comes first.

Code:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	
    	System.out.print("Please enter the first time: ");
    	int t1 = input.nextInt();
    	
    	System.out.print("Please enter the second time: ");
    	int t2 = input.nextInt();
    	input.close();
    	
    	int h1 = t1 / 100;
    	int m1 = t1 % 100 + h1 * 60;
    
    	int h2 = t2 / 100;
    	int m2 = t2 % 100 + h2 * 60;
    
    	if (h1 < h2) {
    	    System.out.println(t1);
    	}
    	else if (h1 == h2) {
    	    if (m1 < m2) {
    		System.out.println(t1);
    	    }
    	    else if (m1 == m2) {
    		System.out.println(t1);
    	    }
    	    else {
    
    	    }
    	}
    	else {
    	    System.out.println(t2);
	}
    }
}