您的位置:首页 > 产品设计 > UI/UE

java生成UUID

2017-10-25 11:07 288 查看
/**
* 直接调用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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: