-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatrij.java
50 lines (35 loc) · 1.26 KB
/
natrij.java
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
45
46
47
package programming_challenges;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class natrij{
public static void main(String[] args) {
Kattio io = new Kattio(System.in);
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
String start = io.getWord();
String end = io.getWord();
try {
Date dStart = format.parse(start);
Date dEnd = format.parse(end);
//reorder
if (dEnd.before(dStart)) dEnd.setTime(dEnd.getTime() + TimeUnit.DAYS.toMillis(1));
//get difference
long diff = dEnd.getTime() - dStart.getTime();
//Convert
long seconds = diff / 1000 % 60;
long minutes = diff / (60 * 1000) % 60;
long hours = diff / (60 * 60 * 1000) % 24;
long[] longTimes = {hours, minutes, seconds};
String[] stringTimes = new String[3];
for(int i = 0; i < 3; i++){
if (longTimes[i] < 10) stringTimes[i] = "0" + longTimes[i];
else stringTimes[i] = longTimes[i] + "";
}
if (diff == 0) System.out.println("24:00:00"); //if the same
else System.out.println(stringTimes[0] + ":" + stringTimes[1] + ":" + stringTimes[2]);
}catch (ParseException e){
}
io.close();
}
}