-
Notifications
You must be signed in to change notification settings - Fork 0
/
string
53 lines (36 loc) · 1.55 KB
/
string
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
48
49
50
51
52
//Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
static String timeConversion(String s) {
String[] str = s.split(":");
int hour = Integer.parseInt(str[0]);
String min = str[1];
String secWithPeriod = str[2];
String sec = str[2].substring(0, secWithPeriod.length() - 2);
String period = str[2].substring(secWithPeriod.length() - 2, secWithPeriod.length());
String militaryTime = "";
if ((0 <= hour && hour < 12) && (period.equalsIgnoreCase("AM"))) {
militaryTime = String.format("%02d", hour) + ":" + min + ":" + sec;
} else if ((0 <= hour && hour < 12) && period.equalsIgnoreCase("PM")) {
militaryTime = (12 + hour) + ":" + min + ":" + sec;
} else if ((hour == 12) && (period.equalsIgnoreCase("AM"))) {
militaryTime = "00" + ":" + min + ":" + sec;
} else if ((hour == 12) && (period.equalsIgnoreCase("PM"))) {
militaryTime = hour + ":" + min + ":" + sec;
}
return militaryTime;
}
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = scan.nextLine();
String result = timeConversion(s);
bw.write(result);
bw.newLine();
bw.close();
}
}