java实现随机生成UUID

时间:2021-07-30 06:35:47

java实现随机生成UUID

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class IDGenerator {
   
  private static long num=0;
   
  /**
   * 随机生成UUID
   * @return
   */
  public static synchronized String getUUID(){
    UUID uuid=UUID.randomUUID();
    String str = uuid.toString();
    String uuidStr=str.replace("-", "");
    return uuidStr;
  }
  /**
   * 根据字符串生成固定UUID
   * @param name
   * @return
   */
  public static synchronized String getUUID(String name){
    UUID uuid=UUID.nameUUIDFromBytes(name.getBytes());
    String str = uuid.toString();
    String uuidStr=str.replace("-", "");
    return uuidStr;
  }
  /**
   * 根据日期生成长整型id
   * @param args
   */
  public static synchronized long getLongId(){
    String date=DateUtil.getDate2FormatString(new Date(), "yyyyMMddHHmmssS");
    System.out.println("原始id="+date);
    if(num>=99) num=0l;
    ++num;
    if(num<10) {
      date=date+00+num;
    }else{
      date+=num;
    }
    return Long.valueOf(date);
  }
  
}

以上所述就是本文的全部内容了,希望大家能够喜欢。