Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create UUID with zeros

Tags:

java

uuid

scala

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

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.

like image 863
Incerteza Avatar asked Dec 30 '13 13:12

Incerteza


People also ask

Is 0 a valid UUID?

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

Can UUID be nil?

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

Can I remove dashes from UUID?

So, removing the dashes won't affect the uniqueness of the UUID. However, it may cause issues with libraries that expect the dashes as part of a UUID to validate it as a UUID.


Video Answer


3 Answers

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

like image 196
Evgeniy Dorofeev Avatar answered Oct 24 '22 14:10

Evgeniy Dorofeev


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

like image 31
Craig Moore Avatar answered Oct 24 '22 13:10

Craig Moore


From 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.

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.

Some tools may accept 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.

like image 2
StephenS Avatar answered Oct 24 '22 14:10

StephenS