Java UUID fromString()方法及示例
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-83e29cd24f4cThe 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-0800200c9a66The UUID from specified String: 58e0a7d7-eebc-11d8-9669-0800200c9a66
