• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generation of a unique identifier  RSS feed

 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to generate a unique id in my system that cannot be guessed by anyone. Is the following logic good enough? After generating the id I will also be hashing this number using SHA1.

public String genarateUId(){
Calendar cal = Calendar.getInstance();
String myId=""+cal.get(Calendar.DATE)+ cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR)+System.currentTimeMillis();
Random randomGenerator=new Random();
int randomInt = randomGenerator.nextInt(10000);
myId=randomInt+myId;
System.out.println(myId);
return myId;
}

Is there a better alternative?

Thanks in advance!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any reason you don't just use java.util.UUID?
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the tip - let me try and figure out if thats what I need.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And try searching these fora; people often ask about unique identifiers. At least once every other month.
 
Cindy Jones
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On reading up on java UUID I read that chances of a duplicate occurring are so slim that it can be ignored.
Is it sufficient to call the method UUID.randomUUID to get a unique Id? I have 2 load balanced servers on which my application will be running.

Thanks
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy Jones wrote:On reading up on java UUID I read that chances of a duplicate occurring are so slim that it can be ignored.
Is it sufficient to call the method UUID.randomUUID to get a unique Id? I have 2 load balanced servers on which my application will be running.

Thanks


That should be fine.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!