Created
February 15, 2016 16:50
-
-
Save husnain-iqbal/aa1fb6f25369ff52b1aa to your computer and use it in GitHub Desktop.
Given a time in AM/PM format, convert it to military (24-hour) time. Note: Midnight is 12:00:00AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock and 12:00:00 on a 24-hour clock. Input Format A time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01≤hh≤1201≤hh≤12. Output Format Convert an…
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
| import java.io.*; | |
| import java.util.*; | |
| import java.text.*; | |
| import java.math.*; | |
| import java.util.regex.*; | |
| public class Solution { | |
| public static void main(String[] args) { | |
| Scanner in = new Scanner(System.in); | |
| String time = in.next(); | |
| if(time.contains("AM")){ | |
| time = time.replace("AM", ""); | |
| String[] timeArr = time.split(":"); | |
| if(timeArr[0].equals("12")){ | |
| timeArr[0] = "00"; | |
| } | |
| time = timeArr[0]+":"+timeArr[1]+":"+timeArr[2]; | |
| } | |
| else if(time.contains("PM")){ | |
| time = time.replace("PM", ""); | |
| String[] timeArr = time.split(":"); | |
| if(!timeArr[0].equals("12")){ | |
| timeArr[0] = Integer.toString(Integer.parseInt(timeArr[0])+12); | |
| } | |
| time = timeArr[0]+":"+timeArr[1]+":"+timeArr[2]; | |
| } | |
| System.out.println(time); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
12:01:02AM