Convert integer minutes into String "hh:mm"
Here you go:
/*Output: 02:05 */
public String fromMinutesToHHmm(int minutes) {
long hours = TimeUnit.MINUTES.toHours(Long.valueOf(minutes));
long remainMinutes = min - TimeUnit.HOURS.toMinutes(hours);
return String.format("%02d:%02d", hours, remainMinutes);
}
Kotlin version
class DateUtils {
companion object {
fun fromMinutesToHHmm(minutes: Int): String {
val hours = TimeUnit.MINUTES.toHours(minutes.toLong())
val remainMinutes = minutes - TimeUnit.HOURS.toMinutes(hours)
return String.format("%02d:%02d", hours, remainMinutes)
}
}
}
String startTime = "00:00";
int minutes = 120;
int h = minutes / 60 + Integer.parseInt(startTime.substring(0,1));
int m = minutes % 60 + Integer.parseInt(startTime.substring(3,4));
String newtime = h+":"+m;
int minutes = 120;
int h = minutes / 60;
int m = minutes % 60;
String.format("%02d:%02d",h,m); // output : "02:00"
String.format("%d:%d",h,m); // output : "2:0"