I'm creating a program to find the time difference between two inputs. The start and the end times are input in 12 hour format such as 9:30am and 11:15am.
I have read the string, use the substring function to extract the string (until the ':') and then converted to integer. The extraction is working perfectly fine.
The problem that I have is with the calculations. e.g. - the time difference between 1:03pm and 12:56pm is 1433 minutes. But my program displays, the difference of 713 minutes.
I have the calculation part of the code up. Any suggestions would be appreciated.
// If-else statements to calculate the total time difference
if((lower1.indexOf('a') == -1) && (lower2.indexOf('a') == -1) && (intMin1 <= 60) && (intMin2 <= 60)){
timeDifference = Math.abs((((intHrs1+12)*60)+intMin1) - (((intHrs2+12)*60)+intMin2));
System.out.println("The time difference is " + timeDifference + " minutes");
} else if((lower1.indexOf('a') != -1) && (lower2.indexOf('a') == -1) && (intMin1 <= 60) && (intMin2 <= 60)) {
timeDifference = Math.abs((((intHrs2+12)*60)+intMin2) - ((intHrs1*60)+intMin1));
System.out.println("The time difference is " + timeDifference + " minutes");
} else if((lower1.indexOf('a') != -1) && (lower2.indexOf('a') != -1) && (intMin1 <= 60) && (intMin2 <= 60)){
timeDifference = Math.abs(((intHrs2*60)+intMin1) - ((intHrs1*60)+intMin2));
System.out.println("The time difference is " + timeDifference + " minutes");
} else if((lower1.indexOf('a') == -1) && (lower2.indexOf('a') != -1) && (intMin1 <= 60) && (intMin2 <= 60)){
timeDifference = Math.abs((((24-(intHrs1+12))*60)+intMin1) - ((intHrs2*60)+intMin2));
System.out.println("The time difference is " + timeDifference + " minutes");
} else if(intMin1 >= 60){
System.out.println("Error: The First time is invalid");
} else {
System.out.println("Error: The second time is invalid");
}