Sunshine
2024-11-05 8f7985d7764a0aad24bd593ac5ea47b7fc290961
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.nova.sankuai.infra.utils.baofu;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
 
public final class FormatUtil {
    /** ==============IS Base=================== */
    /** 判断是否为整数(包括负数) */
    public static boolean isNumber(Object arg) {
        return NumberBo(0, toString(arg));
    }
 
    /** 判断是否为小数(包括整数,包括负数) */
    public static boolean isDecimal(Object arg) {
        return NumberBo(1, toString(arg));
    }
 
    /** 判断是否为空 ,为空返回true */
    public static boolean isEmpty(Object arg) {
        return toStringTrim(arg).length() == 0 ? true : false;
    }
 
    /** ==============TO Base=================== */
    /**
     * Object 转换成 Int 转换失败 返回默认值 0 <br>
     * 使用:toInt(值,默认值[选填])
     */
    public static int toInt(Object... args) {
        int def = 0;
        if (args != null) {
            String str = toStringTrim(args[0]);
            // 判断小数情况。舍弃小数位
            int stri = str.indexOf('.');
            str = stri > 0 ? str.substring(0, stri) : str;
            if (args.length > 1)
                def = Integer.parseInt(args[args.length - 1].toString());
            if (isNumber(str))
                return Integer.parseInt(str);
        }
        return def;
    }
 
    /**
     * Object 转换成 Long 转换失败 返回默认值 0 <br>
     * 使用:toLong(值,默认值[选填])
     */
    public static long toLong(Object... args) {
        Long def = 0L;
        if (args != null) {
            String str = toStringTrim(args[0]);
            if (args.length > 1)
                def = Long.parseLong(args[args.length - 1].toString());
            if (isNumber(str))
                return Long.parseLong(str);
        }
        return def;
    }
 
    /**
     * Object 转换成 Double 转换失败 返回默认值 0 <br>
     * 使用:toDouble(值,默认值[选填])
     */
    public static double toDouble(Object... args) {
        double def = 0;
        if (args != null) {
            String str = toStringTrim(args[0]);
            if (args.length > 1)
                def = Double.parseDouble(args[args.length - 1].toString());
            if (isDecimal(str))
                return Double.parseDouble(str);
        }
        return def;
    }
 
    /**
     * Object 转换成 BigDecimal 转换失败 返回默认值 0 <br>
     * 使用:toDecimal(值,默认值[选填]) 特别注意: new BigDecimal(Double) 会有误差,得先转String
     */
    public static BigDecimal toDecimal(Object... args) {
        return new BigDecimal(Double.toString(toDouble(args)));
    }
 
    /**
     * Object 转换成 Boolean 转换失败 返回默认值 false <br>
     * 使用:toBoolean(值,默认值[选填])
     */
    public static boolean toBoolean(String bool) {
        if (isEmpty(bool) || (!bool.equals("1") && !bool.equalsIgnoreCase("true") && !bool.equalsIgnoreCase("ok")))
            return false;
        else
            return true;
    }
 
    /**
     * Object 转换成 String 为null 返回空字符 <br>
     * 使用:toString(值,默认值[选填])
     */
    public static String toString(Object... args) {
        String def = "";
        if (args != null) {
            if (args.length > 1)
                def = toString(args[args.length - 1]);
            Object obj = args[0];
            if (obj == null)
                return def;
            return obj.toString();
        } else {
            return def;
        }
    }
 
    /**
     * Object 转换成 String[去除所以空格]; 为null 返回空字符 <br>
     * 使用:toStringTrim(值,默认值[选填])
     */
    public static String toStringTrim(Object... args) {
        String str = toString(args);
        return str.replaceAll("\\s*", "");
    }
 
    /** ==============Other Base=================== */
    public static String getNowTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }
 
    /** 数字左边补0 ,obj:要补0的数, length:补0后的长度 */
    public static String leftPad(Object obj, int length) {
        return String.format("%0" + length + "d", toInt(obj));
    }
 
    /** 小数 转 百分数 */
    public static String toPercent(Double str) {
        StringBuffer sb = new StringBuffer(Double.toString(str * 100.0000d));
        return sb.append("%").toString();
    }
 
    /** 百分数 转 小数 */
    public static Double toPercent2(String str) {
        if (str.charAt(str.length() - 1) == '%')
            return Double.parseDouble(str.substring(0, str.length() - 1)) / 100.0000d;
        return 0d;
    }
 
    /**
     * 将byte[] 转换成字符串
     */
    public static String byte2Hex(byte[] srcBytes) {
        StringBuilder hexRetSB = new StringBuilder();
        for (byte b : srcBytes) {
            String hexString = Integer.toHexString(0x00ff & b);
            hexRetSB.append(hexString.length() == 1 ? 0 : "").append(hexString);
        }
        return hexRetSB.toString();
    }
 
    /**
     * 将16进制字符串转为转换成字符串
     */
    public static byte[] hex2Bytes(String source) {
        byte[] sourceBytes = new byte[source.length() / 2];
        for (int i = 0; i < sourceBytes.length; i++) {
            sourceBytes[i] = (byte) Integer.parseInt(source.substring(i * 2, i * 2 + 2), 16);
        }
        return sourceBytes;
    }
 
    /** String 转 Money */
    public static String toMoney(Object str, String MoneyType) {
        DecimalFormat df = new DecimalFormat(MoneyType);
        if (isDecimal(str))
            return df.format(toDecimal(str)).toString();
        return df.format(toDecimal("0.00")).toString();
    }
 
    /** 获取字符串str 左边len位数 */
    public static String getLeft(Object obj, int len) {
        String str = toString(obj);
        if (len <= 0)
            return "";
        if (str.length() <= len)
            return str;
        else
            return str.substring(0, len);
    }
 
    /** 获取字符串str 右边len位数 */
    public static String getRight(Object obj, int len) {
        String str = toString(obj);
        if (len <= 0)
            return "";
        if (str.length() <= len)
            return str;
        else
            return str.substring(str.length() - len, str.length());
    }
 
    /**
     * 首字母变小写
     */
    public static String firstCharToLowerCase(String str) {
        Character firstChar = str.charAt(0);
        String tail = str.substring(1);
        str = Character.toLowerCase(firstChar) + tail;
        return str;
    }
 
    /**
     * 首字母变大写
     */
    public static String firstCharToUpperCase(String str) {
        Character firstChar = str.charAt(0);
        String tail = str.substring(1);
        str = Character.toUpperCase(firstChar) + tail;
        return str;
    }
 
    /**
     * List集合去除重复值 只能用于基本数据类型,。 对象类集合,自己写
     * */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List delMoreList(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        return newList;
    }
 
    public static String formatParams(String message, Object[] params) {
        StringBuffer msg = new StringBuffer();
        String temp = "";
        for (int i = 0; i < params.length + 1; i++) {
            int j = message.indexOf("{}") + 2;
            if (j > 1) {
                temp = message.substring(0, j);
                temp = temp.replaceAll("\\{\\}", FormatUtil.toString(params[i]));
                msg.append(temp);
                message = message.substring(j);
            } else {
                msg.append(message);
                message = "";
            }
        }
        return msg.toString();
    }
 
    /** ============== END =================== */
    public final static class MoneyType {
        /** * 保留2位有效数字,整数位每3位逗号隔开 (默认) */
        public static final String DECIMAL = "#,##0.00";
        /** * 保留2位有效数字 */
        public static final String DECIMAL_2 = "0.00";
        /** * 保留4位有效数字 */
        public static final String DECIMAL_4 = "0.0000";
    }
 
    private static boolean NumberBo(int type, Object obj) {
        if (isEmpty(obj))
            return false;
        int points = 0;
        int chr = 0;
        String str = toString(obj);
        for (int i = str.length(); --i >= 0;) {
            chr = str.charAt(i);
            if (chr < 48 || chr > 57) { // 判断数字
                if (i == 0 && chr == 45) // 判断 - 号
                    return true;
                if (i >= 0 && chr == 46 && type == 1) { // 判断 . 号
                    ++points;
                    if (points <= 1)
                        continue;
                }
                return false;
            }
        }
        return true;
    }
    
    
    /**
     * TreeMa集合2String
     *
     * @param data TreeMa
     * @return String
     */    
    public static String coverMap2String(Map<String, String> data) {
        StringBuilder sf = new StringBuilder();
        for (String key : data.keySet()) {
            if (!isBlank(data.get(key))) {
                sf.append(key).append("=").append(data.get(key).trim()).append("&");
            }
        }
        return sf.substring(0, sf.length() - 1);
    }
 
    /**
     * 空值判断
     * @param cs
     * @return
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 返回参数处理
     * @return
     * @throws Exception 
     */
    public static Map<String,String> getParm(String Rstr) throws Exception{
        Map<String,String> DateArry = new TreeMap<String,String>();
        String[] ListObj=Rstr.split("&");        
        for(String Temp:ListObj){            
            if(Temp.matches("(.+?)=(.+?)")){
                String[] TempListObj=Temp.split("=");                
                DateArry.put(TempListObj[0], TempListObj[1]);               
            }else if(Temp.matches("(.+?)=")){
                String[] TempListObj=Temp.split("=");
                DateArry.put(TempListObj[0],"");
            }else{
                throw new Exception("参数无法分解!");
            }
        }        
        return DateArry;
    }
    
    /**
     * 获取密钥
     * @param KeyStr
     * @return
     * @throws Exception
     */
    public static String getAesKey(String KeyStr) throws Exception{
        String[] ListKeyObj=KeyStr.split("\\|");
        if(ListKeyObj.length == 2){
            if(!ListKeyObj[1].trim().isEmpty()){
                return ListKeyObj[1];
            }else{
                throw new Exception("Key is Null!");
            }
        }else{
            throw new Exception("Data format is incorrect!");            
        }
    }
    
    /**
     * byte数组转换成十六进制字符串
     * @param bArray
     * @return
     */
    public static final String bytesToHexString(byte[] bArray) {
          StringBuffer sb = new StringBuffer(bArray.length);
          String sTemp;
          for (int i = 0; i < bArray.length; i++) {
           sTemp = Integer.toHexString(0xFF & bArray[i]);
           if (sTemp.length() < 2)
            sb.append(0);
           sb.append(sTemp.toUpperCase());
          }
          return sb.toString();
   }
    
    
    /**
     * 把16进制字符串转换成字节数组
     * @return byte[]
     */
    public static byte[] hexString2Bytes(String hex) {
        
        if ((hex == null) || (hex.equals(""))){
            return null;
        }
        else if (hex.length()%2 != 0){
            return null;
        }
        else{                
            hex = hex.toUpperCase();
            int len = hex.length()/2;
            byte[] b = new byte[len];
            char[] hc = hex.toCharArray();
            for (int i=0; i<len; i++){
                int p=2*i;
                b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1]));
            }
          return b;
        }
    }
    
        /*
         * 字符转换为字节
         */
        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
         }
        
        /**
         * 获得4位随机数
         * @return
         */
        public static int getRandNum() {
            int Start = 1000;
            int End = 9999;
            Random random = new Random();
            int number = random.nextInt(End-Start +1)+Start;
            return number;
        }
        
        public static String CreateAeskey() {            
            String randKey = getRandNum()+""+System.currentTimeMillis()+""+getRandNum()+"";            
            String md5Key = SecurityUtil.MD5(randKey);
            return md5Key.substring(0, 16).toUpperCase();
        }
        
}