三七 5 дней назад
Родитель
Сommit
02be9802a9

+ 22 - 6
fs-admin/src/main/java/com/fs/live/controller/LiveAutoTaskController.java

@@ -193,21 +193,37 @@ public class LiveAutoTaskController extends BaseController
     }
 
     public static Date formatTime(String duration) {
-        String[] parts = duration.split(":");
-        if (parts.length != 3) {
+
+        if (duration == null || duration.trim().isEmpty()) {
+            throw new IllegalArgumentException("时间字符串不能为空");
+        }
+
+        // 1. 去除首尾空格
+        String cleaned = duration.trim();
+
+        // 2. 只保留数字和冒号
+        cleaned = cleaned.replaceAll("[^0-9:]", "");
+
+        // 3. 验证格式
+        if (!cleaned.matches("\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
             throw new IllegalArgumentException("时间格式不正确,应为 hh:mm:ss");
         }
+
+        String[] parts = cleaned.split(":");
         int hours = Integer.parseInt(parts[0]);
         int minutes = Integer.parseInt(parts[1]);
         int seconds = Integer.parseInt(parts[2]);
 
-        // 获取当前日期的Calendar实例
+        // 4. 验证数值范围
+        if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) {
+            throw new IllegalArgumentException("时间数值超出有效范围");
+        }
+
         Calendar calendar = Calendar.getInstance();
-        // 设置时分秒
-        calendar.set(Calendar.HOUR_OF_DAY, hours); // 24小时制
+        calendar.set(Calendar.HOUR_OF_DAY, hours);
         calendar.set(Calendar.MINUTE, minutes);
         calendar.set(Calendar.SECOND, seconds);
-        calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0
+        calendar.set(Calendar.MILLISECOND, 0);
 
         return calendar.getTime();
     }