buongiorno, sto scrivendo un programmino che mi converte l'orario (12h e 24h).
Il risultato che voglio ottenere è il seguente:
convertTime("12:00 am") ➞ "0:00"

convertTime("6:20 pm") ➞ "18:20"

convertTime("21:00") ➞ "9:00 pm"

convertTime("5:05") ➞ "5:05 am"


questo è il mio codice, purtroppo il risultato non è quello aspettato, infatti

Un input di tempo di 12 ore sarà indicato con un suffisso am o pm .
Un tempo di input di 24 ore non contiene suffisso.

apprezzerei tanto un aiutino, grazie in anticipo!



public static String convertTime(String time) {
String hour = time.substring(0, time.indexOf(":"));
String min = time.substring(3, time.indexOf(":") + 3);

int hourInteger = Integer.parseInt(hour);

if (hourInteger > 12 && hourInteger < 24) {
hourInteger = hourInteger - 12;
}


if (hourInteger == 24) {
hourInteger = 0;
}
if (hourInteger < 12) {
return hourInteger + ":" + min + " AM";
}
if (hourInteger > 12)
return hourInteger + ":" + min + " PM";

return time;
}