-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeCalculator.java
More file actions
44 lines (40 loc) · 1.19 KB
/
TimeCalculator.java
File metadata and controls
44 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
class TimeCalculator {
public static void main(String[] args) {
System.out.print("Please enter the first time: ");
Scanner in1 = new Scanner(System.in); //Scanner
int time1 = in1.nextInt();
System.out.print("Please enter the second time: ");
Scanner in2 = new Scanner(System.in);
int time2 = in2.nextInt();
int hour1 = time1/100; int min1 = time1 - (hour1*100); //Parsing hour and minute
int hour2 = time2/100; int min2 = time2 - (hour2*100);
int hour_out = 0; int min_out = 0; //Output initialize
if (hour1 <= hour2){ //If first time <= second time
if (min2 >= min1){
hour_out = hour2 - hour1;
min_out = min2 - min1;
}
else {
hour2 -= 1;
min2 += 60;
hour_out = hour2 - hour1;
min_out = min2 - min1;
}
}
else { //If first time > second time
if (min2 >= min1) {
hour2 = 24 + hour2;
hour_out = hour2 - hour1;
min_out = min2 - min1;
}
else {
hour2= 24 + hour2 - 1;
min2 += 60;
hour_out = hour2 - hour1;
min_out = min2 - min1;
}
}
System.out.print("The duration between these two times is " + hour_out + " hours " + min_out + " minutes.");
}
}