秒转换成分钟和小时主要通过取余和取商实现,然后通过StringBuffer拼接显示;代码简单直接上代码
public static String formatTimeS(long seconds) {
int temp = 0;
StringBuffer sb = new StringBuffer();
if (seconds > 3600) {
temp = (int) (seconds / 3600);
sb.append((seconds / 3600) < 10 ? "0" + temp + ":" : temp + ":");
temp = (int) (seconds % 3600 / 60);
changeSeconds(seconds, temp, sb);
} else {
temp = (int) (seconds % 3600 / 60);
changeSeconds(seconds, temp, sb);
}
return sb.toString();
}
private static void changeSeconds(long seconds, int temp, StringBuffer sb) {
sb.append((temp < 10) ? "0" + temp + ":" : "" + temp + ":");
temp = (int) (seconds % 3600 % 60);
sb.append((temp < 10) ? "0" + temp : "" + temp);
}