Skip to main content
☕

How to Generate UUID in Java

Using java.util.UUID

How to Generate UUID in Java. Call UUID.randomUUID() to generate a Java UUID in one line. Before running an INSERT, creating the value in application code is the recommended route in the sqlite uuid reference for SQLite.

Generate UUID in Java

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

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.

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. Without a wrapper, UUID.fromString() throws IllegalArgumentException, so a scala uuid parser should turn failures into a Some or None.

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. If you want to generate uuid python from random bits, call uuid.uuid4(), which draws on os.urandom() rather than a MAC address.

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

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!