Java has a built-in UUID class for version 4. It covers most needs out of the box; time-ordered UUID v7 requires a small, well-maintained library.
Key takeaways
- UUID.randomUUID() generates a version 4 UUID with no dependencies.
- UUID.fromString() parses and validates canonical UUID text.
- For UUID v7, use a maintained library such as uuid-creator.
Generate and parse with the JDK
The standard library covers UUID v4 and parsing. fromString throws IllegalArgumentException for invalid input, which you can catch to validate.
import java.util.UUID;
UUID id = UUID.randomUUID(); // version 4
String text = id.toString();
UUID parsed = UUID.fromString(text); // throws if invalidUUID v7 in Java
The JDK does not generate UUID v7. Add a maintained library when you need time-ordered keys for databases and logs.
// com.github.f4b6a3:uuid-creator
import com.github.f4b6a3.uuid.UuidCreator;
UUID v7 = UuidCreator.getTimeOrderedEpoch();