User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 374,512 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,857 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Apr 20th, 2008
Views: 1,808
This class Generate A unique string. You can concatenate Timestamp to this Unique String and use it as Unique ID
java Syntax
  1. package test;
  2.  
  3.  
  4.  
  5. /*
  6. * @author NAPSTAR
  7.  */
  8.  
  9. import java.util.Random;
  10.  
  11. public class XUIDGenerator {
  12. private static final int NUM_CHARS = 10;
  13. private static String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  14. private static Random r = new Random();
  15.  
  16.  
  17. public static String getUniqueID() {
  18. char[] buf = new char[NUM_CHARS];
  19.  
  20. for (int i = 0; i < buf.length; i++) {
  21. buf[i] = chars.charAt(r.nextInt(chars.length()));
  22. }
  23.  
  24. return new String(buf);
  25. }
  26.  
  27. public static void main(String[] args) {
  28. System.out.println(XUIDGenerator.getUniqueID());
  29. }
  30. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 2:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC