当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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-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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 UUID fromString() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。