Skip to content

Generate UUID in Java

The simplest way to generate a UUID in Java is to use a built-in UUID and call its static method randomUUID. Note that this will produce a valid version 4 UUID.

java
import java.util.UUID;

class UUIDGenerator {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println("Generated UUID: " + uuid.toString());
    }
}

If you need to generate a specific UUID version, you need to use a third-party library such as uuid-creator which offers more flexible options:

java
//generate UUID version 1
UUID uuid = UuidCreator.getTimeBased();

//generate UUID version 2
UUID uuid = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_PERSON, 1234);

//generate UUID version 3
UUID uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, "https://github.com/");

//generate UUID version 4
UUID uuid = UuidCreator.getRandomBased();

//generate UUID version 5
UUID uuid = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, "https://github.com/");

//generate UUID version 6
UUID uuid = UuidCreator.getTimeOrdered();

//generate UUID version 7
UUID uuid = UuidCreator.getTimeOrderedEpoch();

Installation & Setup

Maven Dependency:

xml
<dependency>
    <groupId>com.github.f4b6a3</groupId>
    <artifactId>uuid-creator</artifactId>
    <version>5.3.2</version>
</dependency>

Gradle Dependency:

gradle
implementation 'com.github.f4b6a3:uuid-creator:5.3.2'

Advanced Java Usage

UUID Utility Class:

java
import java.util.UUID;
import com.github.f4b6a3.uuid.UuidCreator;

public class UUIDService {
    
    public static String generateV4() {
        return UUID.randomUUID().toString();
    }
    
    public static String generateV7() {
        return UuidCreator.getTimeOrderedEpoch().toString();
    }
    
    public static boolean isValidUUID(String uuid) {
        try {
            UUID.fromString(uuid);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }
    
    public static int getVersion(String uuid) {
        return UUID.fromString(uuid).version();
    }
}

Spring Boot Integration:

java
@RestController
@RequestMapping("/api")
public class UUIDController {
    
    @GetMapping("/uuid")
    public Map<String, String> generateUUID() {
        Map<String, String> response = new HashMap<>();
        response.put("uuid", UUID.randomUUID().toString());
        response.put("timestamp", Instant.now().toString());
        return response;
    }
}

UUID Version Comparison

Choose the right version for your Java application:

  • Version 1 - Time-based, includes MAC address
  • Version 3 - MD5 namespace-based, deterministic
  • Version 4 - Random, most popular choice
  • Version 5 - SHA-1 namespace-based, more secure than v3
  • Version 6 - Time-ordered, better than v1 for databases
  • Version 7 - Modern time-based with improved sorting

For Java applications:

  • Spring Boot APIs: Use Version 4 for entity IDs
  • Microservices: Consider Version 7 for distributed tracing
  • Enterprise systems: Use Version 5 for deterministic requirements

How do I generate UUID in other languages?

JVM ecosystem:

  • Kotlin - Android and server development
  • Scala - Big data and functional programming
  • Groovy - Scripting and Grails framework
  • Clojure - Functional programming on JVM

Other enterprise languages:

← Back to Online UUID Generator