将秒转换为人类可读格式MM:SS Java

时间:2014-06-05 15:53:14

标签: java time format string-formatting

如何将long int of seconds转换为人类可读的格式

MM:SS

只有SS应填0,所以

long = 67 -> 1:07

4 个答案:

答案 0 :(得分:10)

String readable = String.format("%d:%02d", s/60, s%60);

答案 1 :(得分:2)

使用整数除以60将秒数转换为整分钟。 60秒的模数(%)将给出剩余的"剩余的"秒。然后使用字符串转换来检查是否需要0填充。

int minutes = (total / 60);
int seconds = (total % 60);
String secs = Integer.toString(seconds);
if (seconds < 10) {
   secs = "0" + seconds;
}
String time = minutes + ":" + secs;

答案 2 :(得分:0)

一些算术:

long min = value / 60;
long second = value % 60 ;
String value = min + ":" + (second > 10 ? "" : "0") + second;

答案 3 :(得分:0)

int duration = 90;
int min = duration/60;
int sec = duration%60;
String formattedTime = String.format("%d:%02d",min,sec);