Java中UUID類的fromString()方法用於根據標準字符串表示形式創建UUID。
用法:
public static UUID fromString(String UUID_name)
參數:該方法采用一個參數UUID_name,它是UUID的字符串表示形式。
返回值:該方法返回從指定字符串創建的實際UUID。
異常:如果傳遞了無效的UUID_name,則該方法將引發IllegalArgumentException。
以下示例程序旨在說明fromString()方法的用法:
示例1:
// Java code to illustrate fromString() method
import java.util.*;
public class UUID_Demo {
public static void main(String[] args)
{
// Get the string
String UUID_name
= "5fc03087-d265-11e7-b8c6-83e29cd24f4c";
// Displaying the UUID
System.out.println("The specified String is: "
+ UUID_name);
// Creating the UUID
UUID UUID_1
= UUID
.fromString(UUID_name);
// Displaying the UUID
System.out.println("The UUID from"
+ " specified String: "
+ UUID_1);
}
}
輸出:
The specified String is: 5fc03087-d265-11e7-b8c6-83e29cd24f4c The UUID from specified String: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
示例2:
// Java code to illustrate fromString() method
import java.util.*;
public class UUID_Demo {
public static void main(String[] args)
{
// Get the string
String UUID_name
= "58e0a7d7-eebc-11d8-9669-0800200c9a66";
// Displaying the UUID
System.out.println("The specified String is: "
+ UUID_name);
// Creating the UUID
UUID UUID_1
= UUID
.fromString(UUID_name);
// Displaying the UUID
System.out.println("The UUID from"
+ " specified String: "
+ UUID_1);
}
}
輸出:
The specified String is: 58e0a7d7-eebc-11d8-9669-0800200c9a66 The UUID from specified String: 58e0a7d7-eebc-11d8-9669-0800200c9a66
相關用法
- Java UUID timestamp()用法及代碼示例
- Java UUID compareTo()用法及代碼示例
- Java UUID clockSequence()用法及代碼示例
- Java UUID equals()用法及代碼示例
- Java UUID getMostSignificantBits()用法及代碼示例
- Java UUID node()用法及代碼示例
- Java UUID getLeastSignificantBits()用法及代碼示例
- Java UUID version()用法及代碼示例
- Java UUID variant()用法及代碼示例
- Java UUID hashCode()用法及代碼示例
- Java UUID nameUUIDFromBytes()用法及代碼示例
- Java UUID toString()用法及代碼示例
- Java Java.util.UUID用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 UUID fromString() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。