【发布时间】:2014-07-26 17:11:19
【问题描述】:
如何将 long int of seconds 转换为人类可读的格式
MM:SS
只有 SS 应该是 0 填充所以
long = 67 -> 1:07
【问题讨论】:
-
使用
/和%operator。
标签: java time format string-formatting
如何将 long int of seconds 转换为人类可读的格式
MM:SS
只有 SS 应该是 0 填充所以
long = 67 -> 1:07
【问题讨论】:
/ 和%operator。
标签: java time format string-formatting
一些算术:
long min = value / 60;
long second = value % 60 ;
String value = min + ":" + (second > 10 ? "" : "0") + second;
【讨论】:
使用整数除以 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;
【讨论】:
int
int duration = 90;
int min = duration/60;
int sec = duration%60;
String formattedTime = String.format("%d:%02d",min,sec);
【讨论】:
String readable = String.format("%d:%02d", s/60, s%60);
【讨论】: