UUID UUID Generator

Free · No signup · No install

UUID in Java

The JDK gives you UUID.randomUUID() for a version 4 identifier and UUID.nameUUIDFromBytes() for a deterministic one. Version 7, the RFC 9562 time-ordered format, needs a small library — here is what to use and why.

  • No dependency for v4
  • Deterministic v3 included
  • v7 via a library

UUID v4 Generator

v4
Between 1 and 1000.
Formatting

Result 0

    Generated locally with the Web Crypto API — nothing leaves your browser. Shortcut: Ctrl + Enter — Regenerated

    What is a UUID in Java

    UUID.randomUUID() has been in java.util since Java 5. It returns a variant-2 version-4 UUID built from a cryptographically strong generator, and it is the correct default for request ids, entity keys and correlation ids in logs. Note the variant: the JDK emits the IETF variant, not the older Microsoft layout, so the 17th hex digit is always in 8b.

    For a value that must be reproducible, UUID.nameUUIDFromBytes(bytes) hashes arbitrary bytes with MD5 into a version 3 UUID — the same input gives the same identifier in every JVM. There is no built-in v5 (SHA-1) or v7 in the JDK, so if you want time-ordered keys that keep B-tree inserts sequential you need a library such as uuid-creator, whose UuidCreator.getTimeOrderedEpoch() produces v7 values your database can index in order.

    How to

    1. Call UUID.randomUUID() for a random v4 identifier.
    2. Use UUID.nameUUIDFromBytes(byte[]) when the identifier must be derived from a natural key.
    3. Add uuid-creator when you need v7 and want inserts to stay ordered.

    Use cases

    • Entity and request ids in Spring and Jakarta EE applications, where a random v4 avoids a database round trip.
    • Deterministic dedupe keys: hashing a composite business key gives an id that is stable across services.
    • Correlation ids in logs and tracing headers, where a compact hexadecimal id is easier to grep than a verbose alternative.

    Compare UUID in Java with other formats

    OptionWhen to use
    UUID.randomUUID()v4, cryptographically strong, built into the JDK.
    UUID.nameUUIDFromBytes()v3, MD5 of the input, deterministic but not collision-resistant against crafted input.
    uuid-creator (library)Adds v6, v7 and v8; <code>getTimeOrderedEpoch()</code> gives v7.
    Database identity columnSequential, but requires the database and leaks row counts.

    Code examples

    Random v4

    import java.util.UUID;
    
    UUID id = UUID.randomUUID();
    System.out.println(id);          // 550e8400-e29b-41d4-a716-446655440000
    System.out.println(id.version());  // 4
    System.out.println(id.variant());  // 2

    Deterministic v3

    import java.util.UUID;
    import java.nio.charset.StandardCharsets;
    
    UUID stable = UUID.nameUUIDFromBytes(
        "user:42".getBytes(StandardCharsets.UTF_8));
    // same input -> same UUID in every JVM

    Time-ordered v7 (library)

    // implementation 'com.github.f4b6a3:uuid-creator:6.0.0'
    import com.github.f4b6a3.uuid.UuidCreator;
    
    UUID v7 = UuidCreator.getTimeOrderedEpoch();
    // 019535d9-3df7-79fb-b466-fa907fa17f9e
    
    // Values created later sort later, which keeps index inserts sequential

    Parsing and validating

    import java.util.UUID;
    
    try {
        UUID parsed = UUID.fromString(input);
        // well-formed **and** legal hex
    } catch (IllegalArgumentException notAUuid) {
        // reject the input
    }

    Frequently asked questions

    Does Java have a built-in UUID v7?

    No. The JDK ships v3 (nameUUIDFromBytes) and v4 (randomUUID) only. For v7 you need a third-party library — uuid-creator is the most common — or you generate the value in the database, for example with PostgreSQL 18's uuidv7().

    Is UUID.randomUUID() unique enough for a primary key?

    Statistically yes: 122 random bits give the same collision odds as any other v4 generator. The practical argument against v4 keys is not collisions but locality — random values scatter across a B-tree index, which is exactly what v7 fixes.

    Can I store a UUID in a long or byte array?

    Yes. UUID exposes getMostSignificantBits() and getLeastSignificantBits(), so it fits two longs or, more compactly, 16 bytes in a BINARY(16) column — half the size of the 36-character string.