|
|
|
|
@ -140,4 +140,46 @@ public class TimeUtil {
|
|
|
|
|
return duration; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 将秒数格式化为时分秒字符串 |
|
|
|
|
* @param seconds 秒数 |
|
|
|
|
* @return 时分秒字符串,如:2时30分15秒、15分30秒、45秒 |
|
|
|
|
*/ |
|
|
|
|
public static String formatDuration(long seconds) { |
|
|
|
|
if (seconds <= 0) { |
|
|
|
|
return "0"; |
|
|
|
|
} |
|
|
|
|
// 秒
|
|
|
|
|
if (seconds < 60) { |
|
|
|
|
return seconds + "秒"; |
|
|
|
|
} |
|
|
|
|
// 分钟
|
|
|
|
|
if (seconds < 3600) { |
|
|
|
|
return (seconds / 60) + "分" + (seconds % 60) + "秒"; |
|
|
|
|
} |
|
|
|
|
// 小时
|
|
|
|
|
if (seconds < 86400) { |
|
|
|
|
return (seconds / 3600) + "时" + ((seconds % 3600) / 60) + "分"; |
|
|
|
|
} |
|
|
|
|
// 天
|
|
|
|
|
return (seconds / 86400) + "天" + ((seconds % 86400) / 3600) + "时"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 判断指定时长是否超时(以 1 天为边界) |
|
|
|
|
* @param usedSeconds 已用时长(秒),例如从任务开始到现在的总耗时 |
|
|
|
|
* @return 格式:"未超时/剩余时分秒" 或 "已超时/超出时分秒" |
|
|
|
|
* 剩余/超出时长通过 {@link #formatDuration(long)} 格式化 |
|
|
|
|
*/ |
|
|
|
|
public static String getTimeoutStatus(long usedSeconds) { |
|
|
|
|
long limit = SECONDS_OF_A_DAY; // 86400秒 = 1天
|
|
|
|
|
long diff = limit - usedSeconds; // diff > 0 表示剩余时间,diff < 0 表示超出时间
|
|
|
|
|
if (diff >= 0) { |
|
|
|
|
return "未超时/" + formatDuration(diff); |
|
|
|
|
} else { |
|
|
|
|
return "已超时/" + formatDuration(-diff); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|