Objective:
The goal of this ‘time conversion‘ problem is to convert a given time in a 12-hour AM/PM format to a 24-hour military time format. You are given a time in 12-hour AM/PM format, and you need to convert it to 24-hour military time format.
Input Format:
- In this time conversion problem a single string
s
representing the time in a 12-hour clock format (i.e., hh:mm:ssAM or hh:mm:ssPM), where:hh
is the hour (01 to 12)mm
is the minutes (00 to 59)ss
is the seconds (00 to 59)- The last two characters indicate AM or PM
Output Format:
- In time conversion a single string representing the time in 24-hour military format (i.e.,
HH:MM:SS
), where:HH
is the hour (00 to 23)MM
is the minutes (00 to 59)SS
is the seconds (00 to 59)
Example:
- If the input string is 07:05:45PM, the output should be
19:05:45
. - If the input string is 12:40:22AM, the output should be
00:40:22
.
Steps to Solve Time Conversion:
- Extract the Parts: Separate the hours, minutes, seconds, and period (AM/PM) from the input string.
- Convert Hour:
- If the period is AM:
- If the hour is 12, set the hour to 00.
- Otherwise, keep the hour unchanged (convert to two-digit format if necessary).
- If the period is
PM
:- If the hour is 12, keep the hour unchanged.
- Otherwise, add 12 to the hour (to convert it to 24-hour format).
- If the period is AM:
- Reconstruct the Time: Combine the converted hour, minutes, and seconds into the final 24-hour format string.
Edge Cases to Consider:
- 12:00:00AM should convert to 00:00:00.
- 12:00:00PM should convert to 12:00:00.
Time Conversion HackerRank Problem Snippet:
Time Conversion Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
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) {
// Write your code here
String response = "";
String[] store = s.substring(0, 8).split(":");
String hour = store[0];
if (s.contains("AM")) {
response = (store[0].equals("12") ? "00" : hour) + ":" + store[1] + ":" + store[2];
} else {
int hour2 = Integer.parseInt(hour);
if (hour2 < 12)
hour2 += 12;
response = hour2 + ":" + store[1] + ":" + store[2];
}
return respons;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = bufferedReader.readLine();
String result = Result.timeConversion(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
theproblemsolution.com