-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdUtilsDemo.java
More file actions
63 lines (49 loc) · 1.44 KB
/
IdUtilsDemo.java
File metadata and controls
63 lines (49 loc) · 1.44 KB
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
package com.hutool;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
public class IdUtilsDemo {
public static void main(String[] args) {
new IdUtilsDemo().snowFlake();
}
/**
* 获取随机UUID
*/
public void uuidDemo() {
String uuid = IdUtil.randomUUID();
//格式:cbb021c7-cb48-44cd-b8ba-814620ee4340
System.out.println(uuid);
}
/**
* 简化的UUID,去掉了横线
*/
public void simpleUUIDDemo() {
String uuid = IdUtil.simpleUUID();
//格式:a7e0edfb17ac4120a03842f938f88d34
System.out.println(uuid);
}
/**
* 雪花算法。获取唯一id。
*/
public void getNextIdDemo() {
long id = IdUtil.getSnowflakeNextId();
//格式:1648328806748430336
System.out.println(id);
String idStr = IdUtil.getSnowflakeNextIdStr();
//格式:"1648328806752624640"
System.out.println(idStr);
}
/**
* 雪花算法。配合终端ID和数据中心ID。
*/
public void snowFlake() {
//workerId 终端ID
// datacenterId 数据中心ID
Snowflake snowflake = IdUtil.getSnowflake(1, 1);
long id = snowflake.nextId();
//格式:1648328965712121856
System.out.println(id);
String idStr = snowflake.nextIdStr();
//格式:1648328965716316160
System.out.println(idStr);
}
}