java uuid 字符串_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值