UUID in Java

Java UUID Generator

One static method covers the common case in Java: UUID.randomUUID() returns a v4 value with no imports beyond java.util.

java.util.UUIDv4 built inv7 via library

Generated locally with the Web Crypto API — nothing leaves your browser.

Generating UUIDs in Java

How to generate

  1. Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
  2. Copy one value with Copy, or set How many and use Copy all for a fixture list.
  3. Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.

Use Cases

How Java UUID Generator Compares

UUID.randomUUID()v4, in the JDK since 5, CSPRNG-backed
java-uuid-generatorAll RFC 9562 versions including time-based v7
UUID.nameUUIDFromBytes()v3-style MD5 name hashing — legacy, prefer a v5 library

Code Examples

Java
import java.util.UUID;

UUID id = UUID.randomUUID();        // v4
System.out.println(id);             // 6b3f0c1e-9a4d-4e2b-8f77-2c1d5a6b7c8d
System.out.println(id.toString());  // same, canonical lowercase

// v7 is not in the JDK — add com.fasterxml.uuid:java-uuid-generator
// UUID v7 = Generators.timeBasedEpochGenerator().generate();
Validation
public static boolean isUuid(String s) {
    try {
        UUID.fromString(s);
        return true;
    } catch (IllegalArgumentException e) {
        return false;
    }
}

Frequently Asked Questions

Does Java have UUID v7?
Not in the JDK. Use java-uuid-generator (Generators.timeBasedEpochGenerator()) or uuid-creator, or generate v7 in the database with PostgreSQL 18's uuidv7().
Is UUID.randomUUID() secure enough for tokens?
Yes — it is backed by a cryptographically strong PRNG, which is why it is used for session identifiers. Use SecureRandom directly only if you need bytes rather than a UUID.
How do I store a UUID in Java?
Keep it as a java.util.UUID in the model and let the driver map it: PostgreSQL uuid, MySQL BINARY(16), SQL Server UNIQUEIDENTIFIER.

Related tools