whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/util/ActionUtil.java
@@ -19,6 +19,9 @@
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
@@ -204,6 +207,7 @@
         userInf.setUName("未登录的用户账号");
         userInf.setUId(0L);
         userInf.setURole(0);
         userInf.setUpassword("123456");
       }else{
         userInf=(UserInf) session.getAttribute("user");
       }
@@ -604,6 +608,18 @@
      return lang;
   }
   //获取当前系统的语言环境
   public static String getLang4Socket(HttpSession httpSession) {
      Locale locale = Locale.getDefault();//对Locale类实例化定义
      String lang = locale.getLanguage();
      //String lang = "zh";
      String str = (String) httpSession.getAttribute("lang");
      if (str != null) {
         lang = str;
      }
      return lang;
   }
   /**
    * 输入验证:路径遍历,防止恶意符号影响文件体系
    * 过滤掉特殊字符 ”/\" : | * ? < >”
@@ -615,4 +631,67 @@
      return FilenameUtils.getName(fileName);
   }
   /**
    * 输入验证:路径遍历,防止恶意符号影响文件体系
    * 过滤掉特殊字符 ”/\" : | * ? < >”
    */
   public static boolean filterPwd(String pwd){
      String regex="^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+\\-])[a-zA-Z\\d!@#$%^&*()_+\\-]{8,}$";
      Pattern pattern = Pattern.compile(regex);
      Matcher matcher = pattern.matcher(pwd);
      return matcher.matches();
   }
   /**
    * 将时间字符串(格式HH:mm:ss)转换为秒数
    *
    * @param timeStr 时间字符串
    * @return 转换后的秒数
    */
   public static int convertToSeconds(String timeStr) {
      // 使用冒号(:)分割时间字符串
      String[] parts = timeStr.split(":");
      // 验证时间字符串是否有效(假设总是包含三个部分)
      if (parts.length != 3) {
         throw new IllegalArgumentException("时间字符串格式不正确,应为HH:mm:ss");
      }
      // 解析小时、分钟和秒
      int hours = Integer.parseInt(parts[0]);
      int minutes = Integer.parseInt(parts[1]);
      int seconds = Integer.parseInt(parts[2]);
      // 转换为总秒数
      // 注意:1小时 = 3600秒,1分钟 = 60秒
      int totalSeconds = hours * 3600 + minutes * 60 + seconds;
      return totalSeconds;
   }
   public static String parseDurationDirectly(String timeStr) {
      // 获取当前时间
      LocalDateTime now = LocalDateTime.now();
      String[] parts = timeStr.split(":");
      if (parts.length != 3) {
         throw new IllegalArgumentException("时间长度字符串格式不正确");
      }
      int hours = Integer.parseInt(parts[0]);
      int minutes = Integer.parseInt(parts[1]);
      int seconds = Integer.parseInt(parts[2]);
      Duration timeToAdd = Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
      // 将时间长度加到当前时间上
      LocalDateTime updatedTime = now.plus(timeToAdd);
      return  updatedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
   }
   public static void main(String[] args) {
      //System.out.println(filterPwd("Aa@123456"));
      // 时间长度字符串
      String timeToAddStr = "00:00:34";
      String time =parseDurationDirectly(timeToAddStr);
      System.out.println(time);
   }
}