Converting seconds to human readable format MM:SS Java [duplicate]

孤者浪人 提交于 2020-01-03 18:36:40

问题


How can I convert a long int of seconds to the human readable format

MM:SS

Only SS should be 0 padded so

long = 67 -> 1:07

回答1:


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



回答2:


Use integer division by 60 to turn seconds in to whole minutes. The Modulus(%) of seconds by 60 will give the "leftover" seconds. Then use a string conversion to check for the necessity of 0 padding.

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



回答3:


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



回答4:


Some arithmetic :

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


来源:https://stackoverflow.com/questions/24064759/converting-seconds-to-human-readable-format-mmss-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!