UUID.fromString(String) in Java

UUID.fromString(String): This method is available in java.util.UUID class of Java.

Syntax:

UUID java.util.UUID.fromString(String name)

This method takes one argument. This method creates a UUID from the string standard representation as described in the toString method.

Parameters: One parameter is required for this method.

name: A string that specifies a UUID.

Returns: A UUID with the specified value.

Throws:

IllegalArgumentException - If the name does not conform to the string representation as described in toString

Approach 1: When no exception

Java

import java.util.UUID;

public class UUIDfromString {
    public static void main(String[] args) {

        UUID uuid =
UUID.fromString("3800-8cf0-11bd-b23e-10b96e4ef00d");

        System.out.println(uuid);
    }
}

Output:

00003800-8cf0-11bd-b23e-10b96e4ef00d


Approach 2: IllegalArgumentException

Java

import java.util.UUID;

public class UUIDfromString {
    public static void main(String[] args) {

        UUID uuid = UUID.fromString("3800-b23e-10b96e4ef00d");

        System.out.println(uuid);
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid UUID string: 3800-b23e-10b96e4ef00d at java.base/java.util.UUID.fromString1(UUID.java:280) at java.base/java.util.UUID.fromString(UUID.java:258)


Some other methods of UUID

clockSequence()This method returns the clock sequence value associated with this UUID.

compareTo(UUID)This method compares this UUID with the specified UUID.

equals(Object)This method compares this object to the specified object.

UUID.fromString(String)This method creates a UUID from the string standard representation as described in the toString method.

getLeastSignificantBits()This method returns the least significant 64 bits of this UUID's 128-bit value.

getMostSignificantBits()This method returns the most significant 64 bits of this UUID's 128-bit value.

hashCode()This method returns a hash code for this UUID.

UUID.nameUUIDFromBytes(byte[])Static factory to retrieve a type 3 UUID based on the specified byte array.

node()The node value associated with this UUID.

UUID.randomUUID()Static factory to retrieve a type 4 UUID.

timestamp()This method returns the timestamp value associated with this UUID.

toString()This method returns a String object representing this UUID.

variant()This method returns the variant number associated with this UUID.

version()This method returns the version number associated with this UUID.

No comments:

Post a Comment