|
|
@@ -192,4 +192,37 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
public static String format(Date date, String dateFormat) {
|
|
|
return DateFormatUtils.format(date, dateFormat);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化时间长度(秒转为可读格式,自动省略为 0 的单位)
|
|
|
+ * @param timeLenSeconds 时间长度(秒)
|
|
|
+ * @return 格式化后的时间字符串,如:1 天 2 小时 3 分 4 秒、36 秒、1 小时 5 分等
|
|
|
+ */
|
|
|
+ public static String formatTimeLength(long timeLenSeconds) {
|
|
|
+ if (timeLenSeconds <= 0) {
|
|
|
+ return "0 秒";
|
|
|
+ }
|
|
|
+
|
|
|
+ long days = timeLenSeconds / (24 * 3600);
|
|
|
+ long hours = (timeLenSeconds % (24 * 3600)) / 3600;
|
|
|
+ long minutes = (timeLenSeconds % 3600) / 60;
|
|
|
+ long seconds = timeLenSeconds % 60;
|
|
|
+
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+
|
|
|
+ if (days > 0) {
|
|
|
+ result.append(days).append("天");
|
|
|
+ }
|
|
|
+ if (hours > 0) {
|
|
|
+ result.append(hours).append("小时");
|
|
|
+ }
|
|
|
+ if (minutes > 0) {
|
|
|
+ result.append(minutes).append("分");
|
|
|
+ }
|
|
|
+ if (seconds > 0 || result.length() == 0) {
|
|
|
+ result.append(seconds).append("秒");
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
}
|