jinlin
2025-03-21 77d58298d00c11ade8862ca8acb0fdef5a45322e
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
package com.example.server.utils;
 
/**
 * User
 * Date 2016/11/2.
 */
 
import java.util.Date;
import java.util.Random;
import java.util.UUID;
 
public class UUIDUtil {
 
    public UUIDUtil() {
    }
 
    /**
     * 获得一个UUID
     *
     * @return String UUID
     */
    public static String getUUID() {
        String s = UUID.randomUUID().toString();
        // 去掉“-”符号
        return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
                + s.substring(19, 23) + s.substring(24);
    }
 
    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};
 
    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
 
    }
 
    public static String generateCode() {
        java.text.SimpleDateFormat sfdate = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String code = sfdate.format(new Date());
        float fl = new Random().nextFloat();
        fl = fl < 0.1 ? (float) (fl + 0.1) : fl;
        Integer ran = Integer.parseInt((fl * 1000000 + "").split("\\.")[0]);
        char[] temp = code.substring(code.length() - 6, code.length()).toCharArray();
        for (char c : temp) {
            int i = new Random().nextInt(6);
            ran += Integer.parseInt(((Integer.parseInt(c + "") * Math.pow(10, i)) + "").split("\\.")[0]);
        }
        code = code.substring(0, code.length() - 6) + ran;
        String buwei = "";
        int codel = code.length();
        if (codel < 24) {
            double f = Math.pow(10, 24 - codel);
            fl = new Random().nextFloat();
            fl = fl < 0.1 ? (float) (fl + 0.1) : fl;
            String a1 = fl * f + "";
            buwei = a1.split("\\.")[0];
        }
        return code + buwei;
    }
 
    public static String createVerifyCode() {
        String result = null;
 
        try {
            Random rad = new Random();
            result = rad.nextInt(1000000) + "";
 
            if (result.length() != 6) {
                return createVerifyCode();
            }
 
        } catch (Exception e) {
            System.err.println("create verify code error::" + e);
        }
        return result;
    }
 
    public static Long generateId() {
        String code = String.valueOf(new Date().getTime());
        float fl = new Random().nextFloat();
        fl = fl < 0.1 ? (float) (fl + 0.1) : fl;
        Integer ran = Integer.parseInt((fl * 1000000 + "").split("\\.")[0]);
        char[] temp = code.substring(code.length() - 6, code.length()).toCharArray();
        for (char c : temp) {
            int i = new Random().nextInt(6);
            ran += Integer.parseInt(((Integer.parseInt(c + "") * Math.pow(10, i)) + "").split("\\.")[0]);
        }
        code = code.substring(0, code.length() - 6) + ran;
        String buwei = "";
        int codel = code.length();
        if (codel < 17) {
            double f = Math.pow(10, 17 - codel);
            fl = new Random().nextFloat();
            fl = fl < 0.1 ? (float) (fl + 0.1) : fl;
            String a1 = fl * f + "";
            buwei = a1.split("\\.")[0];
        }
        Random runnable = new Random();
        int num = runnable.nextInt(90) + 10;
        return Long.parseLong(code + buwei + num);
    }
 
    /**
     * 获得指定数目的UUID
     *
     * @param number int 需要获得的UUID数量
     * @return String[] UUID数组
     */
    public static String[] getUUID(int number) {
        if (number < 1) {
            return null;
        }
        String[] ss = new String[number];
        for (int i = 0; i < number; i++) {
            ss[i] = getUUID();
        }
        return ss;
    }
 
    public static String getOrderCode() {
        java.text.SimpleDateFormat sfdate = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String code = sfdate.format(new Date()) + (int) (Math.random() * 9000 + 1000);
        return code;
    }
}