| | |
| | | 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; |
| | |
| | | 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")); |
| | | //System.out.println(filterPwd("Aa@123456")); |
| | | |
| | | // 时间长度字符串 |
| | | String timeToAddStr = "00:00:34"; |
| | | String time =parseDurationDirectly(timeToAddStr); |
| | | System.out.println(time); |
| | | } |
| | | |
| | | } |