-
Notifications
You must be signed in to change notification settings - Fork 0
/
java_12to24h_timeformat.java
48 lines (38 loc) · 1.13 KB
/
java_12to24h_timeformat.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
48
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
/*
* Complete the timeConversion function below.
*/
static String timeConversion(String s) {
/*
* Write your code here.
* 07:05:45PM
* 07 - h
* 45PM - ap_pm
*/
Integer h = new Integer(s.split(":")[0]);
String am_pm = s.split(":")[2].substring(1,3);
if(am_pm == "AM"){
return s.substring(0,s.indexOf("A"));
}
else{
h += 12;
String min = new String(s.split(":")[1]);
String sec = new String(s.split(":")[2].substring(0,2));
return new String(h+":"+min+":"+sec);
}
}
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();
}
}