
To create a time-based UUID, we’ll have: UUID uuid = Generators.timeBasedGenerator().generate() Note that this library provides various types of UUID generators. Let’s start by adding a dependency to java-uuid -generator in our POM: We can also query the variant and the version of a UUID: UUID uuid = UUID.randomUUID() Long mostSignificantBits = uuid.getMostSignificantBits() īoth of these methods return a long value. Long leastSignificantBits = uuid.getLeastSignificantBits() getLeastSignificantBits() And getMostSignificantBits():Īs the name suggests, getLeastSignificantBits() and getMostSignificantBits() return the 64 least-significant and 64 most-significant bits respectively: UUID uuid = UUID.randomUUID() Let’s cover a few other methods of the Java UUID class: 1. We can optionally use the equals() method for comparison as well. -1: if uuid1 is less than that of uuid2.So, we can use the compareTo()method to compare them: UUID uuid1 = UUID.randomUUID() Īs we know, the compareTo() method returns: Java UUID class implements Comparable interface.

It’ll throw an IllegalArgumentException for any invalid string passed in as an argument. With fromString(), we can create a UUID from a standard string representation: UUID uuid = omString("533a4559-e55c-18b3-2456-555563322002") This method generates a v3 UUID (name-based). UUID uuid = UUID.nameUUIDFromBytes(byteArr) We can generate a UUID from a byte array using nameUUIDFromBytes(): byte byteArr = It generates a v4 pseudo-random UUIDusing a cryptographically strong pseudo-random number generator: UUID uuid = UUID.randomUUID() Let’s cover the methods in the Java UUID class which we can use to generate the UUID: 1. That means our UUID has a variant of 2.įor variant 2 UUIDs, there’re five different versions: Version Reserved, Microsoft Corporation backward compatibilityįor us, A = 8 (1000), so the first three MSBs are 100. Here, the value of A represents the variant and is determined by its first three MSBs (Most-Significant bits): However, its constructor allows for generating any type of UUID: new UUID(long mostSigBits, long leastSigBits)Ī variant determines the layout of the UUID. Java UUID class has a method for manipulating the Leach-Salz variant (variant 2). Another popular usage of UUID is for generating primary key values in the database. We can use UUID class for generating a random file name, a session or a transaction id. class in Java represents an immutable UUID. The standard representation of UUID is made up of hexadecimal digits: 533a4559-e55c-18b3-8456-555563322002Īnd has 36 characters that include four hyphens ‘-‘. It’s also popularly known as GUID (Globally Unique Identifier). Return Long.toHexString(MSB | ng.nextLong()) + Long.toHexString(MSB | ng.UUID (Universal Unique Identifier) represents a 128-bit long unique value. NumberGenerator = ng = new SecureRandom() Private static volatile SecureRandom numberGenerator = null

Maxim: Copied from UUID implementation :) Note that I'm not generating a UUID, instead just a random 32 bytes hex string in the most efficient way I could think of.

Ended up writing something of my own based on UUID.java implementation.
