一、使用工具類UUID
使用工具類UUID生成隨機的唯一字符,並且可以轉換成適合的格式。
1 package com.learn; 2 3 import org.junit.Test; 4 5 import java.util.UUID; 6 7 /** 8 * 創建32位唯一字符 9 * 10 * @author zhongtao on 2018/5/15 11 */ 12 public class BuildUUID { 13 14 /** 15 * 生成唯一字符 帶有 - 符號 16 */ 17 @Test 18 public void test1() { 19 String uuid = UUID.randomUUID().toString(); 20 System.out.println(uuid); 21 //d099ed23-8af5-41fc-8e20-76ed66fe3889 22 } 23 24 /** 25 * 替換-符號,並將字母轉換爲大寫 26 */ 27 @Test 28 public void test2() { 29 String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase(); 30 System.out.println(uuid); 31 //2E29DA7EF45B4A8883A55C4161884C28 32 } 33 34 }