puzhibing
2023-11-25 53e7558400dcacecdce70e39ebfe1727740f9296
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
package com.dsh.course.util;
 
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
 
/**
 * 定义生成随机码的工具类
 */
public class UUIDUtil {
 
    private int i = 1;
 
 
    /**
     * 定义生成原生的UUID随机码
     *
     * @return
     */
    public static String getNativeUUID() {
        return UUID.randomUUID().toString();
    }
 
 
    /**
     * 生成32位随机码
     *
     * @return
     */
    public static String getRandomCode() {
        return UUIDUtil.getNativeUUID().replaceAll("-", "");
    }
 
 
    /**
     * 获取给定长度的随机码
     *
     * @param num
     * @return
     * @throws Exception
     */
    public static String getRandomCode(Integer num) throws Exception {
        String str = null;
        if (0 < num) {
            if (num % 32 > 0) {
                Integer s = num / 32;
                Integer l = num % 32;
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < s; i++) {
                    sb.append(UUIDUtil.getRandomCode());
                }
                sb.append(UUIDUtil.getRandomCode().substring(0, l));
                str = sb.toString();
            } else if (num % 32 == 0) {
                Integer s = num / 32;
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < s; i++) {
                    sb.append(UUIDUtil.getRandomCode());
                }
                str = sb.toString();
            } else {
                str = UUIDUtil.getRandomCode().substring(0, num);
            }
        } else {
            throw new Exception("参数只能大于0");
        }
        return str;
    }
 
 
    /**
     * 获取根据当前时间的字符串数据
     *
     * @return
     */
    public synchronized static String getTimeStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssS");
        return simpleDateFormat.format(new Date());
    }
 
 
    /**
     * @Description: 获取数字随机码
     * @Author pzb
     * @Date 2021/8/11 16:52
     * @Param
     * @Return
     * @Exception
     */
    public static String getNumberRandom(Integer num) {
        if (null == num) {
            num = 32;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            sb.append(Double.valueOf(Math.random() * 10).intValue());
        }
        return sb.toString();
    }
 
 
}