Java 用零创建 UUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20840256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:43:29  来源:igfitidea点击:

Create UUID with zeros

javascalauuid

提问by アレックス

I'm trying to generate a UUID with all zeros:

我正在尝试生成一个全为零的 UUID:

java.util.UUID fromString "00000000-00000000-00000000-00000000"

The error is

错误是

 java.lang.IllegalArgumentException: Invalid UUID string: 00000000-00000000-00000000-00000000
    at java.util.UUID.fromString(UUID.java:194)

What am I doing wrong?

我究竟做错了什么?

I want to create either "MinValue" or "Invalid" UUID.

我想创建“MinValue”或“Invalid”UUID。

采纳答案by Evgeniy Dorofeev

try this

尝试这个

System.out.println(new UUID(0,0));

it prints

它打印

00000000-0000-0000-0000-000000000000

this is the right format to use in UUID.fromString

这是使用的正确格式 UUID.fromString

回答by Craig Moore

Isn't it supposed to be 8-4-4-4-12? like this: 00000000-0000-0000-0000-000000000000

不是应该是 8-4-4-4-12 吗?像这样:00000000-0000-0000-0000-000000000000

回答by StephenS

From https://en.wikipedia.org/wiki/Universally_unique_identifier#Nil_UUID:

来自https://en.wikipedia.org/wiki/Universally_unique_identifier#Nil_UUID

The "nil" UUID, a special case, is the UUID, 00000000-0000-0000-0000-000000000000; that is, all bits set to zero.

“nil”UUID,一个特例,就是UUID,00000000-0000-0000-0000-000000000000;也就是说,所有位都设置为零。

The dashes should follow the normal 8-4-4-4-12 format because that's what the standards say to use and many (most?) tools enforce that on input.

破折号应遵循正常的 8-4-4-4-12 格式,因为这是标准所说的使用格式,并且许多(大多数?)工具在输入时强制执行该格式。

Some tools mayaccept other formats, e.g. 32 hex digits with no dashes, because they just remove the dashes (if present) before validation anyway, but the particular tool you're using is a bit stricter/smarter, which shows that using non-standard formats is a bad habit that will end up biting you.

一些工具可能接受其他格式,例如 32 位没有破折号的十六进制数字,因为它们只是在验证之前删除破折号(如果存在),但您使用的特定工具更严格/更智能,这表明使用非-标准格式是一个坏习惯,最终会咬你。