这期内容当中小编将会给大家带来有关怎么在java中对UUID进行获取,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
UUID:
UUID,是Universally Unique Identifier的缩写,UUID出现的目的,是为了让分布式系统可以不借助中心节点,就可以生成UUID来标识一些唯一的信息。
代码:
import java.util.UUID;
public class UUIDTest {
public static void main(String[] args) {
String uuid1 = "e65deb4c-a110-49c8-a4ef-6e69447968d6";
String uuid2 = "ca4a8a92-d4ed-4fc4-8a4f-345c587fbdcb";
String uuid3 = "e1f15f1d-6edb-4f70-8a05465se273eaf95a";
System.out.println("check > " + uuid1 + " > " + isValidUUID(uuid1));
System.out.println("check > " + uuid2 + " > " + isValidUUID(uuid2));
System.out.println("check > " + uuid3 + " > " + isValidUUID(uuid3));
System.out.println("build a uuid> " + getRandomUUID(null));
System.out.println("build a uuid> " + getRandomUUID(null));
System.out.println("build a uuid> " + getRandomUUID("kangyucheng"));
System.out.println("build a uuid> " + getRandomUUID("kangyucheng"));
}
public static boolean isValidUUID(String uuid) {
// UUID校验
if (uuid == null) {
System.out.println("uuid is null");
}
String regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
if (uuid.matches(regex)) {
return true;
}
return false;
}
public static UUID getRandomUUID(String str) {
// 产生UUID
if (str == null) {
return UUID.randomUUID();
} else {
return UUID.nameUUIDFromBytes(str.getBytes());
}
}
}
测试结果:
check > e65deb4c-a110-49c8-a4ef-6e69447968d6 > true
check > ca4a8a92-d4ed-4fc4-8a4f-345c587fbdcb > true
check > e1f15f1d-6edb-4f70-8a05465se273eaf95a > false
build a uuid>93ce6775-bc89-4849-8ae9-fb305c909700
build a uuid>7a61f2e0-903b-4904-b2bd-a075b19adb8c
build a uuid>8b68aa00-6a79-3cbb-996f-780f464f3aae
build a uuid>8b68aa00-6a79-3cbb-996f-780f464f3aae
上述就是小编为大家分享的怎么在java中对UUID进行获取了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。