Well, I have been looking at ways to generate UIDs in java code (most of them coming to stackoverflow too). The best is to use java's UUID to create unique ids since it uses the timestamp. But my problem is that it is 128-bit long and I need a shorter string, like say 14 or 15 characters. So, I devised the following code to do so.
Date date = new Date();
Long luid = (Long) date.getTime();
String suid = luid.toString();
System.out.println(suid+": "+suid.length() + " characters");
Random rn = new Random();
Integer long1 = rn.nextInt(9);
Integer long2 = rn.nextInt(13);
String newstr = suid.substring(0, long2) + " " + long1 + " " + suid.subtring(long2);
System.out.println("New string in spaced format: "+newstr);
System.out.println("New string in proper format: "+newstr.replaceAll(" ", ""));
Please note that I am just displaying the spaced-formatted and properly-formatted string for comparison with the original string only.
Would this guarantee a 100% unique id each time? Or do you see any possibility the numbers could be repeated? Also, instead of inserting a random number into a random position which "might" create duplicate numbers, I could do it either in the beginning or end. This is to complete the required length of my UID. Although this might probably not work if you need a UID less than 13 characters.
Any thoughts?