I am trying to create a time conversion function that converts 12-hour time formats to 24-hour formats. My code works for all inputs except the following: 12:05:39AM.
I am having trouble understanding what I am doing wrong
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
if (s.substring(8).equals("PM")){
String hour = s.substring(0,2);
if (Integer.parseInt(hour) != 12){
int newHour = (Integer.parseInt(hour) + 12) % 24;
return (newHour + s.substring(2,8));
}
return (hour + s.substring(2,8));
}
else if (s.substring(8).equals("AM")){
String AMhour = s.substring(0,2);
if (Integer.parseInt(AMhour) == 12){
int newHour = ((Integer.parseInt(AMhour) + 12) % 24);
return (newHour + s.substring(2,8));
}
return (AMhour + s.substring(2,8));
}
else {
return("Not found");
}
}
}