-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeConversion.java
45 lines (36 loc) · 1.37 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
43
44
45
package br.com.eduardocintra.easy.timeconversion;
import java.io.*;
import static java.util.stream.Collectors.joining;
class Result {
/*
* Complete the 'timeConversion' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String timeConversion(String s) {
/*
* Please, if this code helps you, leave your star on the repository:
* https://github.com/eduardocintra/hacker-rank-solutions
*/
String format = s.replaceAll("[^APM]", "");
String sWithoutFormat = s.replaceAll("[APM]", "");
String[] aTime = sWithoutFormat.split(":");
int hour = Integer.parseInt(aTime[0]);
if (format.equalsIgnoreCase("PM") && hour != 12) hour += 12;
if (format.equalsIgnoreCase("AM") && hour == 12) hour = 0;
return String.format("%02d", hour) + ":" + aTime[1] + ":" + aTime[2];
}
}
public class TimeConversion {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
String s = bufferedReader.readLine();
String result = Result.timeConversion(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}