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
UUID.randomUUID()is a static factory that uses a cryptographically strong pseudo-random generator, so the value is suitable for tokens as well as keys.UUIDis immutable andtoString()always emits the canonical 8-4-4-4-12 lowercase form;UUID.fromString()parses it back and throwsIllegalArgumentExceptionon malformed input.- The JDK has no version 7. For time-ordered keys in a JPA or Hibernate schema, add a library —
com.fasterxml.uuid:java-uuid-generatororuuid-creator— or let the database do it. That matters at scale, because a random v4 primary key scatters B-tree inserts across the whole index.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Hibernate and JPA entity keys — assign in the constructor or with
@GeneratedValueremoved. - Correlation IDs in MDC so every log line for a request can be grepped by one value.
- Idempotency tokens for message consumers that must ignore duplicate deliveries.
How Java UUID Generator Compares
| UUID.randomUUID() | v4, in the JDK since 5, CSPRNG-backed |
| java-uuid-generator | All 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
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator