Create a Guid in Java

Create a GUID in Java

Have a look at the UUID class bundled with Java 5 and later.

For example:

  • If you want a random UUID you can use the randomUUID method.
  • If you want a UUID initialized to a specific value you can use the UUID constructor or the fromString method.

how to create GUID in java EE

import java.util.UUID;

UUID uuid = UUID.randomUUID();

String randomUUIDString = uuid.toString();

Sequential Guid in Java

See this article: http://www.informit.com/articles/article.aspx?p=25862&seqNum=7 (linked to Page 7).

It contains an algorithm for what the author refers to as "COMB" Guids; I reproduce his code (SQL) below:

SET @aGuid = CAST(CAST(NEWID() AS BINARY(10)) 
+ CAST(GETDATE() AS BINARY(6)) AS UNIQUEIDENTIFIER)

Trivial to convert this to Java, or your desired language. The obvious underlying principle is to make the date a component of the Guid. The entire article is a good read, as he does a nice analysis of the performance of the various approaches.

How to create a UUID or GUID in Java 1.4

I suppose there is no chance convincing the client to get off an unsupported version of Java? Which if the answer is no then your only recourse is to use/modify one of the open source implementations from the web. You mentioned two of them in your question, another one you might want to look at is JUG.

And oh yea, your reference to java.util.UUID failed because it's only available in Java 5 and up.

Good luck!

How do I create a GUID / UUID?

UUIDs (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDentifier), according to RFC 4122, are identifiers designed to provide certain uniqueness guarantees.

While it is possible to implement RFC-compliant UUIDs in a few lines of JavaScript code (e.g., see @broofa's answer, below) there are several common pitfalls:

  • Invalid id format (UUIDs must be of the form "xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx", where x is one of [0-9, a-f] M is one of [1-5], and N is [8, 9, a, or b]
  • Use of a low-quality source of randomness (such as Math.random)

Thus, developers writing code for production environments are encouraged to use a rigorous, well-maintained implementation such as the uuid module.

Is there any way to generate a UUID in Java that is identical to that of the one generated in C#?

TL;DR

If you want your C# and your Java to act exactly the same way (and you are happy with the existing C# behaviour), you'll need to manually re-order some of the bytes in uuid_bytes (i.e. swap some of the entries you identified as out of order).

Additionally, you should not use:

UUID.nameUUIDFromBytes(to_encode.trim().getBytes())

But instead use:

public static String getGuidFromByteArray(byte[] bytes) {
ByteBuffer bb = ByteBuffer.wrap(bytes);
long high = bb.getLong();
long low = bb.getLong();
UUID uuid = new UUID(high, low);
return uuid.toString();
}

Shamelessly stolen from https://stackoverflow.com/a/24409153/34092 :)

Additional Background

In case you weren't aware, when dealing with C#'s GUIDs:

Note that the order of bytes in the returned byte array is different
from the string representation of a Guid value. The order of the
beginning four-byte group and the next two two-byte groups is
reversed, whereas the order of the last two-byte group and the closing
six-byte group is the same. The example provides an illustration.

And:

The order of hexadecimal strings returned by the ToString method
depends on whether the computer architecture is little-endian or
big-endian.

In your C#, rather than using:

Console.WriteLine("Guid: {0}", guid);

you may want to consider using:

Console.WriteLine(BitConverter.ToString(guid.ToByteArray()));

Your existing code calls ToString behind the scenes. Alas, ToString and ToByteArray do not return the bytes in the same order.



Related Topics



Leave a reply



Submit