1

I have String variable movieDuration, which contains value in minutes. Need to convert that to HH:mm format. How should I do it?

Tried to do it as:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
movieDurationFormatted = formatter.format(movieDuration);

But looks like value in minutes is not ok for formatter.

skaffman
  • 390,936
  • 96
  • 800
  • 764
LA_
  • 18,964
  • 54
  • 167
  • 296
  • A `DateFormat` formats a date, not a duration. For the difference, consult a dictionary. For converting minutes into hours I’d recommend checking said dictionary, look for “division.” – Bombe Mar 26 '11 at 12:31
  • So, are you suggesting manually to divide minutes by 60, take integer part as hours, remainder as minutes? – LA_ Mar 26 '11 at 12:44
  • LA_, of course because that is exactly how it’s done. :) – Bombe Mar 26 '11 at 18:27

3 Answers3

15
public static String formatHoursAndMinutes(int totalMinutes) {
    String minutes = Integer.toString(totalMinutes % 60);
    minutes = minutes.length() == 1 ? "0" + minutes : minutes;
    return (totalMinutes / 60) + ":" + minutes;
}
aroth
  • 52,868
  • 20
  • 134
  • 172
3

Check this for reference Minutes to Hours-Minutes..

Community
  • 1
  • 1
Venky
  • 11,044
  • 5
  • 47
  • 66
2

Just use the following method to convert minutes to HH:mm on android?

if you want to process long value then just change the parameter type

public static String ConvertMinutesTimeToHHMMString(int minutesTime) {
    TimeZone timeZone = TimeZone.getTimeZone("UTC");
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    df.setTimeZone(timeZone);
    String time = df.format(new Date(minutesTime * 60 * 1000L));

    return time;
}

Happy coding :)

Monir Zzaman
  • 369
  • 3
  • 7