生成随机UUID并获取版本

时间:2016-11-03 20:58:19

标签: java uuid

我正在尝试为字符串生成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();
}

1 个答案:

答案 0 :(得分:4)

它并不是很清楚您要问的是什么,但以下代码将生成随机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);
   }
}
相关问题