Created
January 19, 2022 19:37
-
-
Save jcolebot/f7d3487e76679f6d16540fc88a2abe10 to your computer and use it in GitHub Desktop.
Military Time Conversion Method in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Given a time in -hour AM/PM format, convert it to military (24-hour) time. | |
| Note: - 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock. | |
| - 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. | |
| Example | |
| Return '12:01:00'. | |
| Return '00:01:00'. | |
| Function Description | |
| Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format. | |
| timeConversion has the following parameter(s): | |
| string s: a time in hour format | |
| Returns | |
| string: the time in hour format | |
| Input Format | |
| A single string that represents a time in -hour clock format (i.e.: or ). | |
| Constraints | |
| All input times are valid | |
| Sample Input 0 | |
| 07:05:45PM | |
| Sample Output 0 | |
| 19:05:45 | |
| 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 milTime = ""; | |
| String hourTime = s.substring(0, 2); | |
| String checkTime = s.substring(8, 10); | |
| String timeMinute = s.substring(2, 8); | |
| if(checkTime.equals("AM")) { | |
| if (hourTime.equals("12")) { | |
| milTime = "00" + timeMinute; | |
| } | |
| else { | |
| milTime = hourTime + timeMinute; | |
| } | |
| } | |
| else if (checkTime.equals("PM")) { | |
| if (hourTime.equals("12")) { | |
| milTime = hourTime + timeMinute; | |
| } | |
| else { | |
| int result = Integer.parseInt(hourTime) + 12; | |
| milTime = String.valueOf(result) + timeMinute; | |
| } | |
| } | |
| return milTime; | |
| // String timeSplit = s.split(":"); | |
| // char characters[] = timeSplit[2].toCharArray(); | |
| // if(timeSplit[2].contains("PM")) { | |
| // int newTime = Integer.parseInt(timeSplit[0]); | |
| // newTime = newTime + 12; | |
| // return newTime + ":" + timeSplit[1] + characters[0] + characters[1]; | |
| // } | |
| // else { | |
| // return timeSplit[0] + ":" + timeSplit[1] + ":" + characters[0] + characters[1]; | |
| // } | |
| } | |
| } | |
| 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(); | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution to a HackerRank problem