liujie
2026-09-15 5a95ec36565d7ecc1a5ecb261f26752c90dec120
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.estate.expert.admin.utils;
 
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalTime;
 
/**
 * MD5加密工具类
 */
public class MD5Utils {
 
    /**
     * 对字符串进行MD5加密
     * @param input 输入字符串
     * @return MD5加密后的字符串
     */
    public static String encrypt(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte hashByte : hashBytes) {
                String hex = Integer.toHexString(0xff & hashByte);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }
 
    /**
     * 验证密码是否匹配
     * @param inputPassword 输入的密码
     * @param storedHash 存储的MD5哈希值
     * @return 是否匹配
     */
    public static boolean verify(String inputPassword, String storedHash) {
        String inputHash = encrypt(inputPassword);
        return inputHash.equals(storedHash);
    }
 
    /**
     * 判断当前时间是否在指定时间段内(字符串参数)
     * @param startTimeStr 开始时间,格式:"HH:mm" 或 "HH:mm:ss"
     * @param endTimeStr 结束时间,格式:"HH:mm" 或 "HH:mm:ss"
     * @return 如果在时间段内返回true,否则返回false
     */
    public static boolean isCurrentTimeInRange(String startTimeStr, String endTimeStr) {
        LocalTime now = LocalTime.now();
        LocalTime startTime = LocalTime.parse(fixTimeFormat(startTimeStr));
        LocalTime endTime = LocalTime.parse(fixTimeFormat(endTimeStr));
 
        return !now.isBefore(startTime) && now.isBefore(endTime);
    }
    private static String fixTimeFormat(String timeStr) {
        // 如果只有小时和分钟,添加秒部分
        if (timeStr.length() == 5 && timeStr.contains(":")) {
            return timeStr + ":00";
        }
        return timeStr;
    }
 
    public static void main(String[] args) {
        boolean currentTimeInRange = isCurrentTimeInRange("09:00", "16:50");
        System.out.println("Current time is in range: " + currentTimeInRange);
    }
}