JDK 26 and earlier

UUID v7 in Java

JDK 26 can build a version 7 UUID from a millisecond timestamp. Earlier JDKs still have UUID.randomUUID(), which is version 4, so version 7 has to be assembled from the bytes.

Generate UUIDs

Version

Use a whole number from 1 to 20.

Generation uses crypto.getRandomValues in this browser. Results are not uploaded. A new batch replaces the list.

Results 0

Choose a version and a count, then generate.

    ofEpochMillis rejects a timestamp that does not fit in 48 bits. If you need several values inside one millisecond to increase, pass a timestamp that you already made monotonic. The method will not do that itself.

    The older snippet reuses one SecureRandom. Creating a new SecureRandom on every call spends extra time seeding. The bit operations match the layout used by this site: random rand_a and rand_b, not a counter.

    JDK 26

    import java.time.Instant;
    import java.util.UUID;
    
    UUID id = UUID.ofEpochMillis(Instant.now().toEpochMilli());

    Earlier JDKs

    import java.security.SecureRandom;
    import java.util.UUID;
    
    final class UuidV7 {
      private static final SecureRandom RANDOM = new SecureRandom();
    
      static UUID create() {
        byte[] bytes = new byte[16];
        RANDOM.nextBytes(bytes);
        long ts = System.currentTimeMillis();
        bytes[0] = (byte) (ts >>> 40);
        bytes[1] = (byte) (ts >>> 32);
        bytes[2] = (byte) (ts >>> 24);
        bytes[3] = (byte) (ts >>> 16);
        bytes[4] = (byte) (ts >>> 8);
        bytes[5] = (byte) ts;
        bytes[6] = (byte) ((bytes[6] & 0x0f) | 0x70);
        bytes[8] = (byte) ((bytes[8] & 0x3f) | 0x80);
        long msb = 0;
        long lsb = 0;
        for (int i = 0; i < 8; i++) msb = (msb << 8) | (bytes[i] & 0xffL);
        for (int i = 8; i < 16; i++) lsb = (lsb << 8) | (bytes[i] & 0xffL);
        return new UUID(msb, lsb);
      }
    }

    Questions

    Which JDK has a standard UUID v7 method?

    JDK 26 adds UUID.ofEpochMillis(long). The method builds a version 7 UUID from the millisecond timestamp you pass and fills the remaining bits from a cryptographic random generator. It does not make the timestamp monotonic for you.

    What about older JDKs?

    UUID.randomUUID() is version 4. For version 7, set the first 48 bits from the Unix millisecond time, set the version nibble to 7, set the two variant bits to 10, and fill the other bits with SecureRandom.

    Does the browser tool use Java?

    No. The generator on this page runs in the browser with crypto.getRandomValues. The Java sample is there to copy into a Java program.