public class UUID extends Object implements Serializable, Comparable<UUID>
这些通用标识符具有不同的变体。此类的方法用于操作 Leach-Salz 变体,不过构造方法允许创建任何 UUID 变体(将在下面进行描述)。
变体 2 (Leach-Salz) UUID 的布局如下: long 型数据的最高有效位由以下无符号字段组成:
0xFFFFFFFF00000000 time_low 0x00000000FFFF0000 time_mid 0x000000000000F000 version 0x0000000000000FFF time_hi
long 型数据的最低有效位由以下无符号字段组成:
0xC000000000000000 variant 0x3FFF000000000000 clock_seq 0x0000FFFFFFFFFFFF node
variant 字段包含一个表示 UUID 布局的值。以上描述的位布局仅在 UUID 的 variant 值为 2(表示 Leach-Salz 变体)时才有效。 *
| Constructor and Description |
|---|
UUID(long mostSigBits,
long leastSigBits)
使用指定的数据构造新的 UUID。
|
| Modifier and Type | Method and Description |
|---|---|
int |
clockSequence()
与此 UUID 相关联的时钟序列值。
|
int |
compareTo(UUID val)
将此 UUID 与指定的 UUID 比较。
|
boolean |
equals(Object obj)
将此对象与指定对象比较。
|
static UUID |
fastUUID()
获取类型 4 UUIDv4(伪随机生成的)UUID 的静态工厂。
|
static UUID |
fromString(String name)
根据
toString() 方法中描述的字符串标准表示形式创建UUID。 |
long |
getLeastSignificantBits()
返回此 UUID 的 128 位值中的最低有效 64 位。
|
long |
getMostSignificantBits()
返回此 UUID 的 128 位值中的最高有效 64 位。
|
int |
hashCode()
返回此 UUID 的哈希码。
|
static UUID |
nameUUIDFromBytes(byte[] name)
根据指定的字节数组获取类型 3 UUIDv3(基于名称的·使用MD5哈希加密)UUID 的静态工厂。
|
long |
node()
与此 UUID 相关的节点值。
|
static UUID |
randomUUID()
获取类型 4 UUIDv4(伪随机生成的)UUID 的静态工厂。
|
static UUID |
randomUUID(boolean isSecure)
获取类型 4 UUIDv4(伪随机生成的)UUID 的静态工厂。
|
static UUID |
randomUUID(Random random)
获取类型 4 UUIDv4(伪随机生成的)UUID 的静态工厂。
|
static UUID |
randomUUID7()
获取随机生成的UUIDv7
【开源之夏】贡献内容,见:https://gitee.com/dromara/hutool/pulls/1263 |
long |
timestamp()
与此 UUID 相关联的时间戳值。
|
String |
toString()
返回此
UUID 的字符串表现形式。 |
String |
toString(boolean isSimple)
返回此
UUID 的字符串表现形式。 |
int |
variant()
与此
UUID 相关联的变体号。 |
int |
version()
与此
UUID 相关联的版本号. |
public UUID(long mostSigBits,
long leastSigBits)
mostSigBits - 用于 UUID 的最高有效 64 位leastSigBits - 用于 UUID 的最低有效 64 位public static UUID fastUUID()
UUIDpublic static UUID randomUUID()
UUIDpublic static UUID randomUUID(boolean isSecure)
isSecure - 是否使用SecureRandom如果是可以获得更安全的随机码,否则可以得到更好的性能UUIDpublic static UUID randomUUID(Random random)
random - 随机数生成器UUIDpublic static UUID nameUUIDFromBytes(byte[] name)
name - 用于构造 UUID 的字节数组。UUIDpublic static UUID randomUUID7()
public static UUID fromString(String name)
toString() 方法中描述的字符串标准表示形式创建UUID。name - 指定 UUID 字符串UUIDIllegalArgumentException - 如果 name 与 toString() 中描述的字符串表示形式不符抛出此异常public long getLeastSignificantBits()
public long getMostSignificantBits()
public int version()
UUID 相关联的版本号. 版本号描述此 UUID 是如何生成的。
版本号具有以下含意:
UUID 的版本号public int variant()
UUID 相关联的变体号。变体号描述 UUID 的布局。
变体号具有以下含意:
UUID 相关联的变体号public long timestamp()
throws UnsupportedOperationException
60 位的时间戳值根据此 UUID 的 time_low、time_mid 和 time_hi 字段构造。
所得到的时间戳以 100 毫微秒为单位,从 UTC(通用协调时间) 1582 年 10 月 15 日零时开始。
时间戳值仅在在基于时间的 UUID(其 version 类型为 1)中才有意义。
如果此 UUID 不是基于时间的 UUID,则此方法抛出 UnsupportedOperationException。
UnsupportedOperationException - 如果此 UUID 不是 version 为 1 的 UUID。public int clockSequence()
throws UnsupportedOperationException
14 位的时钟序列值根据此 UUID 的 clock_seq 字段构造。clock_seq 字段用于保证在基于时间的 UUID 中的时间唯一性。
clockSequence 值仅在基于时间的 UUID(其 version 类型为 1)中才有意义。 如果此 UUID 不是基于时间的 UUID,则此方法抛出 UnsupportedOperationException。
UUID 的时钟序列UnsupportedOperationException - 如果此 UUID 的 version 不为 1public long node()
throws UnsupportedOperationException
48 位的节点值根据此 UUID 的 node 字段构造。此字段旨在用于保存机器的 IEEE 802 地址,该地址用于生成此 UUID 以保证空间唯一性。
节点值仅在基于时间的 UUID(其 version 类型为 1)中才有意义。
如果此 UUID 不是基于时间的 UUID,则此方法抛出 UnsupportedOperationException。
UUID 的节点值UnsupportedOperationException - 如果此 UUID 的 version 不为 1public String toString()
UUID 的字符串表现形式。
UUID 的字符串表示形式由此 BNF 描述:
UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node>
time_low = 4*<hexOctet>
time_mid = 2*<hexOctet>
time_high_and_version = 2*<hexOctet>
variant_and_sequence = 2*<hexOctet>
node = 6*<hexOctet>
hexOctet = <hexDigit><hexDigit>
hexDigit = [0-9a-fA-F]
toString in class ObjectUUID 的字符串表现形式toString(boolean)public String toString(boolean isSimple)
UUID 的字符串表现形式。
UUID 的字符串表示形式由此 BNF 描述:
UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node>
time_low = 4*<hexOctet>
time_mid = 2*<hexOctet>
time_high_and_version = 2*<hexOctet>
variant_and_sequence = 2*<hexOctet>
node = 6*<hexOctet>
hexOctet = <hexDigit><hexDigit>
hexDigit = [0-9a-fA-F]
isSimple - 是否简单模式,简单模式为不带'-'的UUID字符串UUID 的字符串表现形式public int hashCode()
public boolean equals(Object obj)
当且仅当参数不为 null、而是一个 UUID 对象、具有与此 UUID 相同的 varriant、包含相同的值(每一位均相同)时,结果才为 true。
public int compareTo(UUID val)
如果两个 UUID 不同,且第一个 UUID 的最高有效字段大于第二个 UUID 的对应字段,则第一个 UUID 大于第二个 UUID。
compareTo in interface Comparable<UUID>val - 与此 UUID 比较的 UUIDCopyright © 2025. All rights reserved.