wlzboy
5 天以前 fe33646ee6e2d1e57f2b51812e94983a0e9efb04
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.ruoyi.common.utils;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
 
import com.ruoyi.common.utils.DateUtils;
 
/**
 * Map值转换工具类
 * 提供从Map中安全获取各种类型值的方法
 * 
 * @author ruoyi
 */
public class MapValueUtils {
 
    /**
     * 从Map中获取字符串值
     * 
     * @param map Map对象
     * @param key 键
     * @return 字符串值,如果键不存在或值为null则返回null
     */
    public static String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        return value != null ? value.toString() : null;
    }
    
    /**
     * 从Map中获取BigDecimal值
     * 
     * @param map Map对象
     * @param key 键
     * @return BigDecimal值,如果键不存在或值为null则返回null,转换失败也返回null
     */
    public static BigDecimal getBigDecimalValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        try {
            return new BigDecimal(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }
    
    /**
     * 从Map中获取Long值
     * 
     * @param map Map对象
     * @param key 键
     * @return Long值,如果键不存在或值为null则返回null,转换失败也返回null
     */
    public static Long getLongValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Long) {
            return (Long) value;
        }
        try {
            return Long.valueOf(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }
    
    /**
     * 从Map中获取Integer值
     * 
     * @param map Map对象
     * @param key 键
     * @return Integer值,如果键不存在或值为null则返回null,转换失败也返回null
     */
    public static Integer getIntegerValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        try {
            return Integer.valueOf(value.toString());
        } catch (NumberFormatException e) {
            return null;
        }
    }
    
    /**
     * 从Map中获取Date值
     * 
     * @param map Map对象
     * @param key 键
     * @return Date值,如果键不存在或值为null则返回null,如果是字符串会尝试解析
     */
    public static Date getDateValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof Date) {
            return (Date) value;
        }
        // 如果是字符串,尝试解析
        if (value instanceof String) {
            try {
                return DateUtils.parseDate(value.toString());
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
    
    /**
     * 验证日期字符串格式是否有效
     * 
     * @param dateStr 日期字符串
     * @param format 日期格式
     * @return 是否有效
     */
    public static boolean isValidDateFormat(String dateStr, String format) {
        if (StringUtils.isEmpty(dateStr)) {
            return false;
        }
        
        try {
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);
            sdf.setLenient(false);
            sdf.parse(dateStr);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}