java 生成随机 UUID 并获取版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40411109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating a random UUID and getting the version
提问by William Chang
I am trying to generate a UUID to a string, so that when I enter uuidcreaterandom, it will create a random UUID. I have pasted below what I tried, and am I missing something ? Any pointers much appreciated. Thank you..
我正在尝试为字符串生成一个 UUID,以便当我输入 uuidcreaterandom 时,它将创建一个随机 UUID。我已将我尝试过的内容粘贴在下面,我是否遗漏了什么?任何指针都非常感谢。谢谢..
public UUID uuidCreateRandom() {
return UUID.randomUUID();
}
public UUID uuidCreateFromHexString(String uuid) {
return UUID.fromString(uuid);
}
public String uuidCreateRandom(UUID uuid) {
return uuid.toString();
}
public int uuidGetVersion(UUID uuid) {
return uuid.version();
}
public UUID get_uuid() {
return _uuid;
}
public String get_uuidString() {
return _uuidString;
}
public int get_uuidVersion() {
return _uuidVersion;
}
public void set_uuid() {
_uuid = UUID.randomUUID();
}
public void set_uuidString(UUID uuid) {
_uuidString = uuid.toString();
}
public void set_uuidVersion(UUID uuid) {
_uuidVersion = uuid.version();
}
回答by jenglert
It's not super clear what you're asking but the following code will generate a random UUID and get the string representation and version:
不是很清楚你在问什么,但下面的代码将生成一个随机的 UUID 并获取字符串表示形式和版本:
Scanner scanner = new Scanner(System.in);
while (true) {
String line = scanner.nextLine().trim();
if (line.equals("uuidcreaterandom")) {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
System.out.println(str);
}
}