Universally Unique Identifiers (UUIDs) are widely used in applications for uniquely identifying entities such as database records, transactions, and distributed objects. In Java, validating a UUID string is essential to ensure it conforms to the correct format and prevents errors in your application.
This article will guide you through different methods for validating UUIDs in Java using built-in and third-party libraries.
What is a UUID?
A UUID is a 128-bit value typically represented as a 36-character string in the format:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
where:
x
represents hexadecimal digits (0-9, a-f).M
represents the version (1, 2, 3, 4, or 5).N
represents the variant (8, 9, A, or B).
Method 1: Using Java’s Built-in UUID
Class
Java provides a built-in UUID
class that can be used to validate UUID strings by attempting to parse them.
import java.util.UUID;
public class UUIDValidator {
public static boolean isValidUUID(String uuid) {
try {
UUID.fromString(uuid);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
public static void main(String[] args) {
String testUUID = "550e8400-e29b-41d4-a716-446655440000";
System.out.println("Is valid UUID? " + isValidUUID(testUUID));
}
}
Explanation
UUID.fromString(uuid)
attempts to parse the input string.- If the string is a valid UUID, it returns a
UUID
object. - If the string is invalid, an
IllegalArgumentException
is thrown, and we returnfalse
.
Method 2: Using Regular Expressions
A regular expression (regex) can be used to check if a string conforms to the standard UUID format.
import java.util.regex.Pattern;
public class UUIDRegexValidator {
private static final String UUID_REGEX =
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$";
private static final Pattern UUID_PATTERN = Pattern.compile(UUID_REGEX);
public static boolean isValidUUID(String uuid) {
return uuid != null && UUID_PATTERN.matcher(uuid).matches();
}
public static void main(String[] args) {
String testUUID = "550e8400-e29b-41d4-a716-446655440000";
System.out.println("Is valid UUID? " + isValidUUID(testUUID));
}
}
Explanation
- The regex pattern ensures the UUID follows the standard format.
- It checks for a valid version (
1-5
) and variant (8, 9, A, or B
). Pattern.matcher(uuid).matches()
verifies if the input string conforms to the pattern.
Method 3: Using Apache Commons Validator
Apache Commons Validator provides a utility for checking UUID validity.
import org.apache.commons.validator.routines.RegexValidator;
public class ApacheUUIDValidator {
private static final RegexValidator UUID_VALIDATOR = new RegexValidator(
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$");
public static boolean isValidUUID(String uuid) {
return UUID_VALIDATOR.isValid(uuid);
}
public static void main(String[] args) {
String testUUID = "550e8400-e29b-41d4-a716-446655440000";
System.out.println("Is valid UUID? " + isValidUUID(testUUID));
}
}
Explanation
- Uses
RegexValidator
from Apache Commons Validator to check UUID format. - Provides a robust and reusable solution.
- Requires adding the Apache Commons Validator dependency:
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.9</version>
</dependency>
Conclusion
Validating UUIDs in Java can be done using different approaches:
- Built-in
UUID
class: Simple and effective but doesn’t strictly enforce version and variant rules. - Regular Expressions: More control over validation rules but slightly more complex.
- Apache Commons Validator: A third-party solution that provides a cleaner way to validate UUIDs.
Choose the method that best suits your application’s requirements. If you’re dealing with standard UUID parsing, the UUID
class is usually sufficient, but for stricter validation, regex or Apache Commons Validator can be useful.
- Roblox Force Trello - February 25, 2025
- 20 Best Unblocked Games in 2025 - February 25, 2025
- How to Use Java Records to Model Immutable Data - February 20, 2025