public static String secToTime(int seconds) {
if (seconds <= 0) {
return "-";
} else {
int temp = 0;
Boolean flag = false;
StringBuffer sb = new StringBuffer();
// 天
temp = seconds / 3600 / 24;
if (temp > 0) {
sb.append(temp + "天");
flag = true;
}
// 时
temp = seconds / 3600 % 24;
if (flag || temp > 0) {
sb.append(temp + "时");
flag = true;
}
// 分
temp = seconds % 3600 / 60;
if (flag || temp > 0) {
sb.append(temp + "分");
flag = true;
}
// 秒
temp = seconds % 3600 % 60;
if (flag || temp > 0) {
sb.append(temp + "秒");
}
return sb.toString();
}
}
public static void main(String[] args) {
// 秒数
int runTime1 = 148840;
int runTime2 = 48840;
int runTime3 = 840;
System.out.println(secToTime(runTime1));
System.out.println(secToTime(runTime2));
System.out.println(secToTime(runTime3));
}
输出结果:
1天17时20分40秒
13时34分0秒
14分0秒