Utils-生成唯一标识(id,uuid,时间戳)

  根据当前时间的毫秒值,随机数,UUID生产唯一数字的字符串, 短ID。可在项目中作为唯一ID使用,如订单ID,交易ID等。

GenUniqueNumStr

时间戳,UUID, Random Id

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
import java.util.Random;
import java.util.UUID;

public class GenUniqueNumStr {

/**
* 根据当前时间的毫秒值(13位)+3位随机数生成16位的唯一字符串数字;
* System.currentTimeMillis(),是从UTC 1970.1.1 零点到当前时间的毫秒值;
* System.nanoTime();原码说明有可能会越界出现负数,测了好长时间没有出现,谨慎不建议用;
*
* @return String
*/
public static String getRandomNum() {

long millis = System.currentTimeMillis();
Random random = new Random();
int end3 = random.nextInt(999);
// 如果不足三位前面补0
String randomNum = millis + String.format("%03d", end3);
return randomNum;
}

/**
* 生产原生的36位UUID字符串,
*
* @return String
*/
public static String getNativeUUID() {
String uuidStr = UUID.randomUUID().toString();
return uuidStr;
}

/**
* 生产去除连接符(-)的32位UUID字符串;
* 使用String的replace()方法
*
* @return String
*/
public static String getUUIDRep() {
UUID uuid = UUID.randomUUID();
// 去除连接符:-
String uuidStr = uuid.toString().replace("-", "");
return uuidStr;
}

/**
* 生产去除连接符(-)的32位UUID字符串;
* 使用String的split()方法,对切割后的数组进行拼接
*
* @return String
*/
public static String getUUIDSplit() {
String uuidStr = "";
UUID uuid = UUID.randomUUID();
String[] uuidArr = uuid.toString().split("-");
for(int i = 0;i<uuidArr.length;i++)
{
uuidStr = uuidStr + uuidArr[i];
}
return uuidStr;
}

UuidUtils

短UUID,Mac地址

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
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.UUID;

/**
* 为了避免uuid重复特地加上mac地址hash后的值
*/
public class UuidUtil {

private static Logger log = LogManager.getLogger(UuidUtil.class);
private static String HASH_MAC = "";

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" };


/**
* 生成短uuid
* 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。
* 本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,
* 然后通过模62操作,结果作为索引取出字符,
*
* @return
*/
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();
}

/**
* 返回一个混合了mac地址的uuid
*
* @return
*/
public static String getUuidMixMac(){

if("".equals(HASH_MAC)) {
try {
HASH_MAC = getMACAddress().hashCode() + "";
} catch (Exception e) {
log.error(e.getMessage());
}
}
String s = UUID.randomUUID().toString();
return (HASH_MAC+UUID.randomUUID().toString()).replace("-", "");

}

/**
* 获取本机mac地址
*
* @return
* @throws Exception
*/
public static String getMACAddress() throws Exception {
// 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。
byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
// 下面代码是把mac地址拼装成String
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
return sb.toString().toUpperCase();
}

public static void main(String [] arg) throws Exception{

System.out.println(getMACAddress());
System.out.println(getUuidMixMac());
System.out.println(generateShortUuid());
}
}

RandomUtil

生成指定长度随机数, 密钥, MD5

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
import org.springframework.util.DigestUtils;

import java.util.HashSet;
import java.util.Set;

/**
*随机数和秘钥工具类
*/
public class RandomUtil {
//随机数长度
private static final int RANDOM_LENGTH_4 = 4;
private static final int RANDOM_LENGTH_6 = 6;
//========================= 账号秘钥生成 ============================
/**
* 获取随机生成指定长度的随机数
*/
public static String getRandom(int length) {
Set<String> set = new HashSet<String>();
while (set.size() < length) {
java.util.Random r=new java.util.Random();
int nextInt = r.nextInt(10);
set.add(nextInt+"");
}
String number = "";
for (String str : set) {
number += str;
}
return number;
}

/**
* 获取生成的秘钥
*
* @return
*/
public static String getSecretKey(){
String secret = ""; //秘钥
for(int i=0;i<1;i++){
secret = secret+(char) (Math.random ()*26+'A');
}
for(int i=0;i<1;i++){
secret = secret+(char) (Math.random ()*26+'a');
}
String numberTwo = getRandom(RANDOM_LENGTH_4);
secret += numberTwo;
return secret;
}

/**
* md5加密
*
* @return
*/
public static String EncoderByMd5(String str,String salt){
//加密后的字符串
// String newstr = DigestUtils.md5Hex(str + "@" + salt);
String newstr = DigestUtils.md5DigestAsHex((str + "@" + salt).getBytes());
return newstr;
}

public static String getSalt() {
return getRandom(RANDOM_LENGTH_6);
}

public static void main(String[] args) {
System.out.println(getRandom(4));
System.out.println(getSalt());
System.out.println(getSecretKey());
}
}

数字加大小写字母随机字符串

模拟公众号文章链接最后的字符串,22位,表示该文章的唯一标识。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Test
public void random() {
String[] strArr = {"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",
"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", "-", "_",};
int index = 0;
Random random = new Random();
StringBuilder randomStr = new StringBuilder();
for (int i = 0; i < 22; i++) {
if (i == 0) {
//首位不出现"-"和"_"
index = random.nextInt(strArr.length - 2);
} else {
index = random.nextInt(strArr.length);
}
randomStr.append(strArr[index]);
}
System.out.println(randomStr);
}

Utils-生成唯一标识(id,uuid,时间戳)

http://blog.gxitsky.com/2018/01/21/Utils-GenUniqueNum/

作者

光星

发布于

2018-01-21

更新于

2022-06-17

许可协议

评论