java的UUID简单生成 - 果核剥壳

java的UUID简单生成

package com.covenlonki.utils;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;

public class generateUuid {

public static void main(String[] args) {
System.out.println("得到8位的UUID-(码): " + getUUID_8());
System.out.println("得到16位的UUID-(数字): " + getUUID_16());
System.out.println("得到32位的UUID-(码): " + getUUID_32());
}

/*得到16位的UUID-(数字)*/
public static String getUUID_16() {
int machineId = 1;// 最大支持1-9个集群机器部署

int hashCodeV = UUID.randomUUID().toString().hashCode();
if (hashCodeV < 0) {// 有可能是负数
hashCodeV = -hashCodeV;
}
String string = machineId + String.format("%015d", hashCodeV);
return string;
}

/*得到32位的UUID-(码)*/
public static String getUUID_32() {
return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}

public static final 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" };

/*得到8位的UUID-(码)*/
public static String getUUID_8() {
StringBuffer shortBuffer = 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 x = Integer.parseInt(str, 16);
shortBuffer.append(CHARS[x % 0x3E]);
}
return shortBuffer.toString();
}
}

运行结果:

得到8位的UUID-(码): 68yZXVVm
得到16位的UUID-(数字): 1000000966435108
得到32位的UUID-(码): 14690bc5598848a08a23a7335d9a0ab2

如果您喜欢本站,点击这儿不花一分钱捐赠本站

这些信息可能会帮助到你: 下载帮助 | 报毒说明 | 进站必看

修改版本安卓软件,加群提示为修改者自留,非本站信息,注意鉴别

(0)
上一篇 2023年8月1日 上午10:19
下一篇 2023年8月1日 上午10:40

相关推荐

发表回复

评论问题之前,点击我,能帮你解决大部分问题

您的电子邮箱地址不会被公开。 必填项已用*标注