[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Generate unique id

Hi

How can I generate unique id?

Thanks
0
hasozduru
Asked:
hasozduru
  • 4
  • 2
  • 2
1 Solution
 
fargoCommented:
there are many ways of doing it, in my opinion following is the best (orderwise)
* use sequences in database
OR
* may be use like String id =  new Long(System.currentTimeMillis()).toString()
0
 
fargoCommented:
second option can be better replaced with the following
http://www.rgagnon.com/javadetails/java-0385.html
0
 
rrzCommented:
How are you going to use the id ?  Are you naming a file ? If yes, maybe
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21833607.html 
will help you.  You could  just use System.currentTimeMillis() as discussed at the link or you could generate a random number and it would probably be unique every time.     rrz
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
rrzCommented:
sorry fargo I didn't see your posts.  
0
 
hasozduruAuthor Commented:
But there is possibility to create two same id's with millisecond, isn't there?
0
 
fargoCommented:
u need to make it synchronised then. I mean have a method which returns unique id and make it synchronised.
0
 
hasozduruAuthor Commented:
What do you mean? Can you please give me the code to do that?
0
 
fargoCommented:
like it is in the given site link
static long current= System.currentTimeMillis();
// this method is synchronized
static public synchronized long get(){
    return current++;
    }

there is no possibility to create two same ids here...all should be unique.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now