Advertisement
Guest User

TimeBetween.java

a guest
Dec 7th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class TimeBetween{
  3.     public static void main(String[]args){
  4.         //Input
  5.         Scanner scan = new Scanner(System.in);
  6.         System.out.print("Enter first time: ");
  7.         String first = scan.next();
  8.         System.out.print("Enter second time: ");
  9.         String second = scan.next();
  10.        
  11.         int firsthour = 0;
  12.         int firstminute = 0;
  13.         int secondhour = 0;
  14.         int secondminute = 0;
  15.        
  16.         //Substrings
  17.        
  18.         //First Hour Tester
  19.         if(first.substring(0,1).equals("0")){
  20.             firsthour = Integer.parseInt(first.substring(1,2));
  21.         }
  22.         else{
  23.             firsthour = Integer.parseInt(first.substring(0,2));
  24.         }
  25.        
  26.         //First Minute Tester
  27.         if(first.substring(2).equals("00")){
  28.             firstminute = 0;
  29.         }
  30.         else{
  31.             firstminute = Integer.parseInt(first.substring(2));
  32.         }
  33.        
  34.         //Second Hour Tester
  35.         if(second.substring(0,1).equals("0")){
  36.             secondhour = Integer.parseInt(second.substring(1,2));
  37.         }
  38.         else{
  39.             secondhour = Integer.parseInt(second.substring(0,2));
  40.         }
  41.        
  42.         //Second Minute Tester
  43.         if(first.substring(2,3).equals("00")){
  44.             secondminute = 0;
  45.         }
  46.         else{
  47.             secondminute = Integer.parseInt(second.substring(2));
  48.         }
  49.        
  50.         //Calculation
  51.         int hours = secondhour-firsthour;
  52.         int minutes = secondminute-firsthour;
  53.        
  54.         //Output
  55.         System.out.println(hours+":"+minutes); //8:-7 output for 0700 and 1500
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement