Open In App

UUID fromString() Method in Java with Examples

Last Updated : 27 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The fromString() method of UUID class in Java is used for the creation of UUID from the standard string representation of the same.

Syntax:

public static UUID fromString(String UUID_name)

Parameters: The method takes one parameter UUID_name which is the string representation of the UUID.

Return Value: The method returns the actual UUID created from the specified string.

Exception: The method throws IllegalArgumentException if an invalid UUID_name is passed.

Below programs illustrate the working of fromString() method:

Program 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);
    }
}


Output:

The specified String is: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The UUID from specified String: 5fc03087-d265-11e7-b8c6-83e29cd24f4c

Program 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);
    }
}


Output:

The specified String is: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The UUID from specified String: 58e0a7d7-eebc-11d8-9669-0800200c9a66


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads