java生成UUID

来源:互联网 发布:网络安全法出台时间 编辑:程序博客网 时间:2024/06/16 05:18
/**     * 直接调用UUID的randomUUID方法,即可获得UUID对象,然后就获取了这个唯一标识码。     *      * @return     */    public static String getUUID()    {        UUID uuid = UUID.randomUUID();        System.out.println(uuid);        return uuid.toString();    }



 public static String[] chars = new String[]            {                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V","W", "X", "Y", "Z"            };         /**     * 获得较短的uuid     *      * @return     */    public static String getShortUUID(){        StringBuffer stringBuffer = new StringBuffer();         String uuid = UUID.randomUUID().toString().replace("-", "");         for (int i = 0; i < 8; i++)        {             String str      = uuid.substring(i * 4, i * 4 + 4);             int strInteger  = Integer.parseInt(str, 16);             stringBuffer.append(chars[strInteger % 0x3E]);         }                  return stringBuffer.toString();     }


原创粉丝点击