Skip to main content

How to Generate UUID in Java

Using java.util.UUID

Reach for java.util.UUID whenever your application needs a collision-resistant unique identifier — it's shipped in the JDK since version 1.5, so there's nothing to install and nothing to configure. Call UUID.randomUUID() for a random, cryptographically secure UUID in one line, or UUID.fromString() when you need to parse one back from a string. Below you'll find copy-paste ready code for generating a Java UUID, converting a string back into a UUID object, and the third-party library that fills in what the standard library leaves out. The typescript uuid reference shows how to create a UUID branded type to prevent mixing plain strings with UUIDs.

Generate UUID in Java

UUID.randomUUID()
550e8400-e29b-41d4-a716-446655440000

How to Generate a UUID in Java

The standard way to generate a UUID in Java is UUID.randomUUID(), built into the JDK since Java 1.5 — no dependencies, no configuration required. It draws from a cryptographically secure random source to produce a 128-bit identifier with virtually no chance of collision. The python uuid generator guide includes production patterns for FastAPI, SQLAlchemy, and bulk generation scripts.

Main.java
import java.util.UUID;

public class Main {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        // Output: 550e8400-e29b-41d4-a716-446655440000
    }
}

Explanation

Convert a String to a UUID in Java

Sometimes a UUID arrives as a plain string — from a REST request, a config file, or a database column — and you need it back as a proper UUID object. UUID.fromString() handles this in one line. The mysql uuid page includes examples for MySQL 8.0+ with UUID_TO_BIN() for storage-efficient identifiers.

Main.java
import java.util.UUID;

public class Main {
    public static void main(String[] args) {
        UUID original = UUID.randomUUID();
        String uuidString = original.toString();

        // Parse the string back into a UUID object
        UUID parsed = UUID.fromString(uuidString);
        System.out.println(original.equals(parsed));
        // Output: true
    }
}

Explanation

Other Options for Generating UUIDs in Java

java.util.UUID covers random (v4) and name-based (v3) generation, but it has no native support for UUID v7, the newer time-sortable format increasingly used for database primary keys. The third-party uuid-creator library fills that gap.

pom.xml
<dependency>
    <groupId>com.github.f4b6a3</groupId>
    <artifactId>uuid-creator</artifactId>
    <version>5.3.7</version>
</dependency>
Main.java
import com.github.f4b6a3.uuid.UuidCreator;
import java.util.UUID;

public class Main {
    public static void main(String[] args) {
        // UUID v7 - Unix timestamp + random, sortable and index-friendly
        UUID uuidV7 = UuidCreator.getTimeOrderedEpoch();
        System.out.println(uuidV7);
    }
}

Explanation

Comments & Feedback

Share your experience or ask questions about this tool

Copied!