I’m trying to convert time that is stored in a database as type string and print it in the appropriate format. I tried using .replaceAll. This resulted in 5:3 being replaced as 05:03 but also turned 17:3 into 0107:03. Does anyone have any suggestions on the correct approach?
String moduleStartHour = (c.getString(c.getColumnIndex(database.KEY_MODULESTARTHOUR)));
String moduleStartMinute = (c.getString(c.getColumnIndex(database.KEY_MODULESTARTMINUTE)));
String newModuleStartHour= moduleStartHour.replaceAll("0", "00").replaceAll("1", "01").replaceAll("2", "03")
.replaceAll('4', "04").replaceAll("5", "05").replaceAll("6", "06").replaceAll("7", "07").replaceAll("8", "08")
.replaceAll("9", "09");
String newModuleStartMinute= moduleStartMinute.replaceAll("0", "00").replaceAll("1", "01").replaceAll("2", "03")
.replaceAll("4", "04").replaceAll("5", "05").replaceAll("6", "06").replaceAll("7", "07").replaceAll("8", "08")
.replaceAll("9", "09");
StringBuilder sb =new StringBuilder();
sb.append(newModuleStartHour).append(":").append(newModuleStartMinute);
String moduleStartTime = sb.toString();
or if we use the StringBuilder for the entire process