How to Generate UUIDs in Java

Generate UUIDs in Java with UUID.randomUUID() for v4 and a library such as uuid-creator for v7 when you need sortable identifiers.

Recommendation: use UUID v4 for fully random IDs, and UUID v7 when you want time ordering without giving up randomness.

How to generate UUID v4 in Java

import java.util.UUID;

UUID id = UUID.randomUUID();
System.out.println(id);

// Example: 7d444840-9dc0-11d1-b245-5ffdce74fad2

How to generate UUID v7 in Java

The Java standard library does not currently expose UUID v7 directly, so most projects use a library.

// Maven: com.github.f4b6a3:uuid-creator
import com.github.f4b6a3.uuid.UuidCreator;
import java.util.UUID;

UUID id = UuidCreator.getTimeOrderedEpoch();
System.out.println(id);

Native support notes

  • Java uses UUID terminology, not GUID terminology.
  • UUID v4 is native through UUID.randomUUID().
  • UUID v7 normally requires a third-party library.
  • Other UUID versions are not part of the common standard-library workflow.

Practical notes

  • UUID v4 is the simplest option for APIs and distributed systems.
  • UUID v7 is easier to sort and often better for database indexes.
  • Be explicit about which library owns UUID v7 generation in shared Java codebases.

Frequently Asked Questions

Not in the common standard-library API. Most Java projects use a dedicated UUID library for v7 generation.

Use v4 for simple random identifiers. Use v7 when insert order and chronological sorting matter more.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.