Skip to content

Instantly share code, notes, and snippets.

@husnain-iqbal
Created February 15, 2016 16:50
Show Gist options
  • Select an option

  • Save husnain-iqbal/aa1fb6f25369ff52b1aa to your computer and use it in GitHub Desktop.

Select an option

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…
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);
}
}
@nagarajuthiparthi123
Copy link

12:01:02AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment