Java : UUID with Examples

UUID (Java SE 17 & JDK 17) API Examples.
You will find code examples on most UUID methods.


Summary

A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value.

Class diagram

final var uuid1 = UUID.randomUUID();
System.out.println(uuid1); // "4f9d5cb7-9975-4d61-98f7-899c7b66c5e6"

final var uuid2 = UUID.randomUUID();
System.out.println(uuid2); // "a4019056-b6b7-4f67-ade7-dfadb820e8d1"

Constructors

UUID (long mostSigBits, long leastSigBits)

Constructs a new UUID using the specified data.

// An example with a version 1 UUID.
final var uuid = new UUID(0xd31d8b5fe2ff11ebL, 0xa541aaaaaaaaaaaaL);

System.out.println(uuid); // "d31d8b5f-e2ff-11eb-a541-aaaaaaaaaaaa"
System.out.println(uuid.version()); // 1

Methods

int clockSequence ()

The clock sequence value associated with this UUID.

final var uuid = new UUID(0xd31d8b5fe2ff11ebL, 0xa541aaaaaaaaaaaaL);

System.out.println(uuid); // "d31d8b5f-e2ff-11eb-a541-aaaaaaaaaaaa"
System.out.println(uuid.version()); // 1
System.out.println(Long.toHexString(uuid.clockSequence())); // "2541"

int compareTo (UUID val)

Compares this UUID with the specified UUID.

final var uuid1 = UUID.randomUUID();
final var uuid2 = UUID.randomUUID();

System.out.println(uuid1); // "806d3fc7-1082-4e1b-963f-9d1393a154cd"
System.out.println(uuid2); // "a7c47ab4-24bb-4867-9eef-ec6290a07250"

System.out.println(uuid1.compareTo(uuid1)); // 0
System.out.println(uuid1.compareTo(uuid2)); // -1
System.out.println(uuid2.compareTo(uuid1)); // 1

boolean equals (Object obj)

Compares this object to the specified object.

final var uuid1 = UUID.randomUUID();
final var uuid2 = UUID.randomUUID();
final var uuid3 = new UUID(uuid2.getMostSignificantBits(), uuid2.getLeastSignificantBits());

System.out.println(uuid1); // "d9b55327-b047-4118-b9dc-2871a643d4c7"
System.out.println(uuid2); // "4c3051c0-8e2e-4085-965c-22565e7936bd"
System.out.println(uuid3); // "4c3051c0-8e2e-4085-965c-22565e7936bd"

System.out.println(uuid1.equals(uuid2)); // false
System.out.println(uuid1.equals(uuid3)); // false

System.out.println(uuid2 != uuid3); // true
System.out.println(uuid2.equals(uuid3)); // true

static UUID fromString (String name)

Creates a UUID from the string standard representation as described in the toString() method.

final var uuid = UUID.fromString("d9b55327-b047-4118-b9dc-2871a643d4c7");
System.out.println(uuid); // "d9b55327-b047-4118-b9dc-2871a643d4c7"

long getLeastSignificantBits ()

Returns the least significant 64 bits of this UUID's 128 bit value.

final var mostSigBits = 0xd31d8b5fe2ff11ebL;
final var leastSigBits = 0xa541aaaaaaaaaaaaL;

final var uuid = new UUID(mostSigBits, leastSigBits);

System.out.println(uuid); // "d31d8b5f-e2ff-11eb-a541-aaaaaaaaaaaa"
System.out.println(Long.toHexString(uuid.getMostSignificantBits())); // "d31d8b5fe2ff11eb"
System.out.println(Long.toHexString(uuid.getLeastSignificantBits())); // "a541aaaaaaaaaaaa"

System.out.println(mostSigBits == uuid.getMostSignificantBits()); // true
System.out.println(leastSigBits == uuid.getLeastSignificantBits()); // true

long getMostSignificantBits ()

Returns the most significant 64 bits of this UUID's 128 bit value.

Please see getLeastSignificantBits.

int hashCode ()

Returns a hash code for this UUID.

final var uuid1 = UUID.randomUUID();
final var uuid2 = UUID.randomUUID();

System.out.println(uuid1); // "e0e45f19-5e7a-4216-b2f1-6fa4ef0dda58"
System.out.println(uuid2); // "c9de186d-e7cc-425e-bb99-7bdded2b57b6"

System.out.println(uuid1.hashCode()); // -480073485
System.out.println(uuid2.hashCode()); // 2023781976

static UUID nameUUIDFromBytes (byte[] name)

Static factory to retrieve a type 3 (name based) UUID based on the specified byte array.

final var name = "example.com";
final var uuid = UUID.nameUUIDFromBytes(name.getBytes());

System.out.println(uuid); // "5ababd60-3b22-3803-82dd-8d83498e5172"
System.out.println(uuid.version()); // 3

long node ()

The node value associated with this UUID.

final var uuid = new UUID(0xd31d8b5fe2ff11ebL, 0xa541aaaaaaaaaaaaL);

System.out.println(uuid); // "d31d8b5f-e2ff-11eb-a541-aaaaaaaaaaaa"
System.out.println(uuid.version()); // 1
System.out.println(Long.toHexString(uuid.node())); // "aaaaaaaaaaaa"

static UUID randomUUID ()

Static factory to retrieve a type 4 (pseudo randomly generated) UUID.

IntStream.range(0, 10).forEach(i -> {
    final var uuid = UUID.randomUUID();
    System.out.println(uuid);
});

// Result
// ↓
//5406c135-12ca-48cb-9eec-12db24656d59
//ae9903c3-63f2-4e65-a2f8-f560ded28e6e
//3989c048-b9f3-423e-a783-7ebe889cd945
//40ea47fa-ca8f-4a55-be41-d0ae02d748cf
//a5e9c128-3e67-4997-bb84-d70716104d48
//bb0e7ae1-ce38-4cf8-96c9-eaf5a9d2ba08
//f8b156ea-ee1d-4f69-b0f1-651fb8ee7052
//114b86a0-001d-4993-a9e9-08dc8216a44c
//36627b5d-0815-4640-bd34-32336f9aceb0
//29a21c8e-5ef1-40df-b7d9-1ad6bc5de47c

long timestamp ()

The timestamp value associated with this UUID.

final var uuid = new UUID(0xd31d8b5fe2ff11ebL, 0xa541aaaaaaaaaaaaL);

System.out.println(uuid); // "d31d8b5f-e2ff-11eb-a541-aaaaaaaaaaaa"
System.out.println(uuid.version()); // 1

// The timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.
final var timestamp = uuid.timestamp();
System.out.println(timestamp); // 138453801951398751

final var baseTime = LocalDateTime.of(1582, 10, 15, 0, 0, 0);

final var seconds = timestamp / 10000000;
final var duration = Duration.ofSeconds(seconds);

final var time = baseTime.plus(duration);
System.out.println(time); // 2021-07-12T10:56:35

String toString ()

Returns a String object representing this UUID.

final var uuid = UUID.nameUUIDFromBytes("example.com".getBytes());
final var str = uuid.toString();
System.out.println(str); // "5ababd60-3b22-3803-82dd-8d83498e5172"
final var uuid = UUID.randomUUID();
final var str = uuid.toString();
System.out.println(str); // "3c242fc2-dad0-4646-b9ac-fce3110edcfc"

int variant ()

The variant number associated with this UUID.

final var uuid = UUID.nameUUIDFromBytes("example.com".getBytes());

System.out.println(uuid); // "5ababd60-3b22-3803-82dd-8d83498e5172"
System.out.println(uuid.version()); // 3
System.out.println(uuid.variant()); // 2
final var uuid = UUID.randomUUID();

System.out.println(uuid); // "f9efac68-ee3c-4cb4-ae4d-516cefb3704e"
System.out.println(uuid.version()); // 4
System.out.println(uuid.variant()); // 2

int version ()

The version number associated with this UUID.

Please see variant.


Related posts

To top of page