Convert whole minutes to string "hh: mm"

I need to convert minutes (defined as Integer) to the following string format "hh: mm", assuming startTime is "00:00". The code below is what I have so far, but it does not work correctly. Nor does it take into account that newTime should be shifted according to startTime . Is there any other solution?

 String startTime = "00:00"; int minutes = 120; double time = minutes/60; String timeS = Double.toString(time); String[] hourMin = timeS.split("."); String h = hourMin[0]; String m = hourMin[1]; String newTime = ""; newTime.concat(h+":"+m); 
+11
java time
source share
7 answers
 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; 
+8
source share

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) } } } 
+26
source share

I wrote this recently to convert seconds to hours, minutes, and seconds; adapt it as you like (replace "s", "m", "h" with colons, skip seconds, etc.).

 private static String getFormattedTime(int secs) { // int secs = (int) Math.round((double) milliseconds / 1000); // for millisecs arg instead of secs if (secs < 60) return secs + "s"; else { int mins = (int) secs / 60; int remainderSecs = secs - (mins * 60); if (mins < 60) { return (mins < 10 ? "0" : "") + mins + "m " + (remainderSecs < 10 ? "0" : "") + remainderSecs + "s"; } else { int hours = (int) mins / 60; int remainderMins = mins - (hours * 60); return (hours < 10 ? "0" : "") + hours + "h " + (remainderMins < 10 ? "0" : "") + remainderMins + "m " + (remainderSecs < 10 ? "0" : "") + remainderSecs + "s"; } } } 
+2
source share
 String startTime = "00:00"; int minutes = 120; //--- counting int hour = minutes/60; minutes=minutes-hours*60; String m = Integer.toString(minutes); String h = Integer.toString(hours); newTime=h+":"+m; 
0
source share
 int ALLTIME_IN_MINUTES = 1444 int hour = ALLTIME_IN_MINUTES /60; int min = ALLTIME_IN_MINUTES %60; String dateStr = hour+":"+min; SimpleDateFormat curFormater = new SimpleDateFormat("H:m"); Date dateObj = curFormater.parse(dateStr); SimpleDateFormat postFormater = new SimpleDateFormat("HH:mm"); String newDateStr = postFormater.format(dateObj); 

RESULT: 24:04

0
source share

apines' answer is good, but if you want to remove the warning in Android add 'Locale':

 int hours = 3; int minutes = 6; private static final String TIME_SEPARATOR = ":"; String.format(Locale.getDefault(), "%02d"+TIME_SEPARATOR+"%02d", hours, minutes); 
0
source share
 int minutes = 129; double hoursAndMinutes = (minutes / 60) + (double) minutes % 60 / 100; String hoursAndMinutesStr = new DecimalFormat("0.00").format(hoursAndMinutes).replace('.', ':'); System.out.println(hoursAndMinutesStr); 
-2
source share

All Articles