Answer:
see explaination for program code
Step-by-step explanation:
/Convert 12-hour time to 24-hour time format
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
while(!str.equals("99:99AM")){
System.out.println(getConvertedTime(str));
str=br.readLine();
}
}
public static String getConvertedTime(String str){
String s=str.substring(str.length()-2);
String[] hoursAndMinutes=str.substring(0,5).split(":");
if(s.equals("AM")){
//if the time is in AM then we can use same hours and minutes except for 12th hour so we need
//special case for that if it is 12th hour we will use "00" instead of 12 for other we can use same hours
if(hoursAndMinutes[0].equals("12")) return "00"+hoursAndMinutes[1];
else return hoursAndMinutes[0]+hoursAndMinutes[1];
}
else{
//if the time is in PM we need to add hours to 12 unless the hour is 12 in case of 12 we need to keep it as it is
if(hoursAndMinutes[0].equals("12")) return "12"+hoursAndMinutes[1];
else{
int hours=Integer.valueOf(hoursAndMinutes[0]);
hoursAndMinutes[0]=String.valueOf(12+hours);
return hoursAndMinutes[0]+hoursAndMinutes[1];
}
}
}
}