Java implements random generation of uuids

  • 2020-04-01 04:02:33
  • OfStack

Java implements random generation of uuids


public class IDGenerator {
   
  private static long num=0; 
   
  
  public static synchronized String getUUID(){
    UUID uuid=UUID.randomUUID();
    String str = uuid.toString(); 
    String uuidStr=str.replace("-", "");
    return uuidStr;
  }
  
  public static synchronized String getUUID(String name){
    UUID uuid=UUID.nameUUIDFromBytes(name.getBytes());
    String str = uuid.toString(); 
    String uuidStr=str.replace("-", "");
    return uuidStr;
  }
  
  public static synchronized long getLongId(){
    String date=DateUtil.getDate2FormatString(new Date(), "yyyyMMddHHmmssS");
    System.out.println(" The original id="+date);
    if(num>=99) num=0l;
    ++num;
    if(num<10) {
      date=date+00+num;
    }else{
      date+=num;
    }
    return Long.valueOf(date);
  }
  
}

The above is all the content of this article, I hope you can enjoy it.


Related articles: