I had solved it in a different way. Maybe, it helps you. But unfortunately, I couldn't try the code on Android...
Here is my code:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
// other code...
int numberOfHourFrom_0_to_23 = 15;
String hour = numberOfHourFrom_0_to_23 + ""; // you need a string for the following parsing
String mstring = "";
// you have to do it here, because in the other case you do it in the try block,
// you wouldn't have access on it after finishing it
try{
DateFormat hh = new SimpleDateFormat("hh");
// your input date format, in this case only hours
Date date = hh.parse(hour);
/* you have to parse the value of the string to a Date object,
can throw a 'ParseException' exception --> try-catch */
SimpleDateFormat targetFromat = new SimpleDateFormat("hh aa");
// your target format, 'aa' is for AM/PM
mstring = targetFromat.format(date);
// the method formats the time to the new format (incl. AM/PM)
} catch(ParseException e){
// exception threw by parse in the class Date
System.err.println("There was an error during parsing the initial string.");
e.printStackTrace();
}
System.out.println(mstring); // test output, in this case: 03 PM
Maybe is this article interesting for you: https://beginnersbook.com/2017/10/java-display-time-in-12-hour-format-with-ampm/ (it helped me by this answer).
UPDATE:
After reading the comment from @Ole V.V. (thank you for your correction!), I look at the class DateTimeFormatter and I want to add the code here (it is much simpler as my first code!):
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
// other code...
int numberOfHourFrom_0_to_23 = 15;
LocalTime time = LocalTime.of(numberOfHourFrom_0_to_23, 0, 0);
// parameters: hour, minute, second
String formatPattern = "hh a";
// the pattern, "hh" is for the two hours positions and "a" for AM/PM
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern);
String mstring = formatter.format(time);
// formatting the value and solve it as a String
System.out.println(mstring); // test output, in this case: 03 PM