Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

How to make 32 byte UUID - Better Than java.RMI.server.UID

JavaFAQ Home » Java Code Examples Go to all tips in Java Code Examples

Bookmark and Share

Improve the UUID (java.RMI.server.UID) by this excellent solution, code follows...

Personally, I do not like the java.RMI.server.UID solution, because the String representation produces negative numbers.. and it does not appear to be a straight forward operation to extract the values and reformat.

I DO like the proposed design from EJB Design Patterns book, but I have a different implementation that I believe solves the 50-day cycle problem identified by Thomas Paré, where chopping only the significant 8 bytes from the result of CurrentTimeMillis() produces about 50 days worth of unique values. A better approach would be to keep all significant 12 bytes of the CurrentTimeMillis() and keep only 4 bytes from the SecureRandom() function (see below):

  1. (0 - 7) IPAddress as HEX - 8 bytes
  2. (8 - 19) CurrentTimeMillis() as HEX - Display all 12 bytes
  3. (20 - 23) SecureRandom() as HEX - Keep only 4 significant bytes. Since this is "random" it doesn't really matter how many bytes you keep or eliminate
  4. (24 - 31) System.identityHashCode as Hex - 8 bytes



import org.apache.log4j.Category;

import java.net.InetAddress;
import java.security.SecureRandom;

public class UID {
    static Category m_log = Category.getInstance(UID.class.getName());

    public UID() {

    public String getUID() {
        String strRetVal = "";
        String strTemp = "";
        try {
            // Get IPAddress Segment
            InetAddress addr = InetAddress.getLocalHost();

            byte[] ipaddr = addr.getAddress();
            for (int i = 0; i < ipaddr.length; i++) {
                Byte b = new Byte(ipaddr[i]);

                strTemp = Integer.toHexString(b.intValue() & 0x000000ff);
                while (strTemp.length() < 2) {
                    strTemp = '0' + strTemp;
                strRetVal += strTemp;

            strRetVal += ':';

            //Get CurrentTimeMillis() segment
            strTemp = Long.toHexString(System.currentTimeMillis());
            while (strTemp.length() < 12) {
                strTemp = '0' + strTemp;
            strRetVal += strTemp + ':';

            //Get Random Segment
            SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");

            strTemp = Integer.toHexString(prng.nextInt());
            while (strTemp.length() < 8) {
                strTemp = '0' + strTemp;

            strRetVal += strTemp.substring(4) + ':';

            //Get IdentityHash() segment
            strTemp = Long.toHexString(System.identityHashCode((Object) this));
            while (strTemp.length() < 8) {
                strTemp = '0' + strTemp;
            strRetVal += strTemp;

        catch (java.net.UnknownHostException ex) {
            m_log.error("Unknown Host Exception Caught: " + ex.getMessage());
        catch (java.security.NoSuchAlgorithmException ex) {
            m_log.error("No Such Algorithm Exception Caught: " + ex.getMessage());

        return strRetVal.toUpperCase();

    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 10; i++) {
            long lngStart = System.currentTimeMillis();
            UID objUID = new UID();

            m_log.debug("Elapsed time: " + (System.currentTimeMillis() - lngStart));


The performance of this function appears pretty good - operating between 0 and 10ms.

P. S. by alex: This very valuable tip was submitted October 19, 2006. Unfortunately I was extremely occupied during this autumn and had tons of submissions to my sites. This real gold tip was buried under hundreds of spam messages (many sites get it), but finally was found and I publish it today!

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy