huang
2025-10-30 a99650cb00bf5b0650c33f39a4221b765201d228
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.mes.utils;
 
import cn.hutool.core.util.IdUtil;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
 
/**
 * 最nb的工具类
 *
 * @author kong
 */
public class NbUtil {
 
    private static final String STRING_NULL = "null";
 
 
    // 时间处理
 
    /**
     * 返回指定时间的YYYY-MM-dd hh:mm:ss 字符串格式
     */
    public static String getDtString(Date d) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
    }
 
    /**
     * 返回系统当前时间的YYYY-MM-dd hh:mm:ss 字符串格式
     */
    public static String getNow() {
        return getDtString(new Date());
    }
 
    /**
     * 将一个字符串转换为日期格式(YYYY-MM-dd HH:mm:ss)
     */
    public static Date getDt(String d) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(d);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 返回当前时间的指定形式 如(YYYY_MM_dd_HH_mm_ss)
     *
     * @return
     */
    public static String getNowString(String geshi) {
        return new SimpleDateFormat(geshi).format(new Date());
    }
 
    /**
     * 指定日期,指定格式
     */
    public static String getDateString(Date date, String geshi) {
        return new SimpleDateFormat(geshi).format(date);
    }
 
    /**
     * 获取指定日期的1号
     */
    public static Date getYueOne(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
 
 
    //基本字符处理
 
    /**
     * 该字符串是否为null或者空串
     */
    public static boolean isNull(String str) {
        return (str == null || "".equals(str));
    }
 
    /**
     * 该字符串是否为null或者空串
     */
    public static boolean isOneNull(String... str) {
        for (String string : str) {
            if ((string == null || "".equals(string))) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * 如果一个字符串为(null,"","null"),则转化为指定值
     */
    public static String toStr(String str, String toStr) {
        if (str == null || "".equals(str) || STRING_NULL.equals(str)) {
            return toStr;
        }
        return str;
    }
 
    /**
     * 如果该货不能转成一个数字,则返回指定值
     */
    public static Integer toInt(String str, Integer toInt) {
        try {
            return new Integer(str);
        } catch (Exception e) {
            return toInt;
        }
    }
 
    private static Pattern patternNumberPattern = Pattern.compile("[0-9]*");
 
    /**
     * 验证一个str是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        return patternNumberPattern.matcher(str).matches();
    }
 
    private static Pattern patternNumberPhone = Pattern.compile("[1]\\d{10}");
 
    /**
     * 验证一个str是否为手机号
     *
     * @param str
     * @return
     */
    public static boolean isPhone(String str) {
        if (str == null) {
            return false;
        }
        return patternNumberPhone.matcher(str).matches();
    }
 
 
    /**
     * 判断一个数是否在0、1、2、3...10、20、30...100、200、300... 数列里面
     */
    private static Pattern patternSeries = Pattern.compile("[0-9]0*");
 
    public static boolean isSeries(int num) {
        return patternSeries.matcher(num + "").matches();
    }
 
 
    /**
     * 将一个字符串ISO-8859-1转码UTF-8
     */
    public static String toUtf8(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }
 
    /**
     * 将字符串转化为指定数据类型
     */
    @SuppressWarnings("unchecked")
    public static <T> T getObjectByClass(String str, Class<T> cs) {
        Object value = null;
        if (str == null) {
            value = null;
        } else if (cs.equals(String.class)) {
            value = str;
        } else if (cs.equals(int.class) || cs.equals(Integer.class)) {
            value = new Integer(str);
        } else if (cs.equals(long.class) || cs.equals(Long.class)) {
            value = new Long(str);
        } else if (cs.equals(short.class) || cs.equals(Short.class)) {
            value = new Short(str);
        } else if (cs.equals(float.class) || cs.equals(Float.class)) {
            value = new Float(str);
        } else if (cs.equals(double.class) || cs.equals(Double.class)) {
            value = new Double(str);
        } else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
            value = new Boolean(str);
        } else {
            throw new RuntimeException("超纲的类型:" + cs + ",未能转换值:" + str);
        }
        return (T) value;
    }
 
    /**
     * 返回随机数
     */
    public static int getRandom(int min, int max) {
        max = max + 1;
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }
 
    /**
     * 返回随机字符串
     */
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
 
 
    /**
     * 返回唯一标示28位唯一标示符
     */
    public static String getMarking28() {
        return System.currentTimeMillis() + "" + new Random().nextInt(Integer.MAX_VALUE);
    }
 
    /**
     * 取文件后缀
     */
    public static String getSuffixName(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
 
 
    /**
     * 指定Properties配置文件,读取成为Map, 返回null代表无此配置文件
     */
    public static Map<String, String> readPropToMap(String propertiesPath) {
        Map<String, String> map = new HashMap<>(16);
        try {
            InputStream is = NbUtil.class.getClassLoader().getResourceAsStream(propertiesPath);
            if (is == null) {
                return null;
            }
            Properties prop = new Properties();
            prop.load(is);
            for (String key : prop.stringPropertyNames()) {
                map.put(key, prop.getProperty(key));
            }
        } catch (IOException e) {
            throw new RuntimeException("配置文件(" + propertiesPath + ")加载失败", e);
        }
        return map;
    }
 
 
    /**
     * 初始化对象的属性,根据Map,支持直接为类static字段赋值
     */
    public static Object initPropByMap(Map<String, String> map, Object obj) {
        Class<?> cs = null;
        if (obj instanceof Class) {
            // 已经是类
            cs = (Class<?>) obj;
            obj = null;
        } else {
            // 实例对象
            cs = obj.getClass();
        }
 
        for (Field field : cs.getDeclaredFields()) {
            String value = map.get(field.getName());
            if (value == null) {
                // 为空代表没配置此项
                continue;
            }
            try {
                Object valueConvert = getObjectByClass(value, field.getType());
                field.set(obj, valueConvert);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new RuntimeException("属性取值出错:" + field.getName(), e);
            }
        }
        return obj;
    }
 
    /**
     * 返回一个6位数手机验证码
     */
    public static String getcolde() {
        return Double.toString(((Math.random() * 9 + 1) * 100000)).substring(0, 6);
    }
 
    /**
     * 返回雪花算法id
     */
    public static String getSnowflakeId() {
        return IdUtil.getSnowflake(1, 1).nextIdStr();
    }
 
 
}