-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTimeConversion.java
42 lines (32 loc) · 1.25 KB
/
TimeConversion.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
import java.util.Scanner;
public class TimeConversion {
public static String timeConversion(String time) {
String strHour, strMinute, strSecond, strMeridiem;
String result = "";
strHour = time.substring(0, 2);
strMinute = time.substring(3, 5);
strSecond = time.substring(6, 8);
strMeridiem = time.substring(8, 10);
int hour = Integer.parseInt(strHour);
int minute = Integer.parseInt(strMinute);
int second = Integer.parseInt(strSecond);
if (strMeridiem.equalsIgnoreCase("pm") && hour == 12) {
result = String.format("%02d:%02d:%02d", hour + 12, minute, second);
if (hour == 12) {
result = String.format("%02d:%02d:%02d", hour, minute, second);
}
}
if (strMeridiem.equalsIgnoreCase("am")) {
result = String.format("%02d:%02d:%02d", 12 - hour, minute, second);
if (hour < 12) {
result = String.format("%02d:%02d:%02d", hour, minute, second);
}
}
return result;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String time = in.nextLine();
System.out.println(timeConversion(time));
}
}