开发者

How to convert minutes to HH:mm on android?

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.


public static String formatHoursAndMinutes(int totalMinutes) {
    String minutes = Integer.toString(totalMinutes % 60);
    minutes = minutes.length() == 1 ? "0" + minutes : minutes;
    return (totalMinutes / 60) + ":" + minutes;
}


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 :)


Solution on kotlin Documentation

import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration

val min = 150.toDuration(DurationUnit.MINUTES)
val time = min.toComponents { days, hours, minutes, seconds, nanoseconds -> 
 "$days $hours $minutes $seconds $nanoseconds"
}

We get 0 days 2 hours 30 minutes 0 seconds 0 nanoseconds

We can also use

DurationUnit.DAYS
DurationUnit.HOURS
DurationUnit.SECONDS
DurationUnit.MILLISECONDS
DurationUnit.MICROSECONDS
DurationUnit.NANOSECONDS
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜