-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeConversation.java
More file actions
28 lines (26 loc) · 934 Bytes
/
timeConversation.java
File metadata and controls
28 lines (26 loc) · 934 Bytes
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
public class timeConversation {
static String timeConversion(String s) {
String [] tempArray = s.split(":");
String hours = tempArray[0];
String minutes = tempArray[1];
String seconds = tempArray[2].substring(0, 2);
int tempHours;
if (tempArray[2].substring(2, 4).equalsIgnoreCase("PM")) {
if (Integer.parseInt(hours) < 12) {
tempHours = Integer.parseInt(hours);
tempHours += 12;
hours = Integer.toString(tempHours);
}
}
if (tempArray[2].substring(2, 4).equalsIgnoreCase("AM")) {
if (Integer.parseInt(hours) == 12) {
hours = "00";
}
}
return hours + ":" + minutes + ":" + seconds;
}
public static void main(String[]args){
String s = "07:05:45PM"; //sample test case
System.out.println(timeConversion(s));
}
}