Class UuidConverter


  • public final class UuidConverter
    extends Object
    Utility that converts UUIDs to and from strings, byte arrays or other UUID types.
    • Method Detail

      • toBytes

        public static byte[] toBytes​(UUID uuid)
        Get the array of bytes from a UUID.
        Parameters:
        uuid - a UUID
        Returns:
        an array of bytes
      • fromBytes

        public static UUID fromBytes​(byte[] bytes)
        Get a UUID from an array of bytes.
        Parameters:
        bytes - an array of bytes
        Returns:
        a UUID
        Throws:
        InvalidUuidException - if invalid
      • toString

        public static String toString​(UUID uuid)
        Get a string from a UUID. It is much faster than UUID.toString() in JDK 8. In JDK9+ prefer UUID.toString().
        Parameters:
        uuid - a UUID
        Returns:
        a UUID string
      • fromString

        public static UUID fromString​(String string)
        Get a UUID from a string. It accepts strings: - With URN prefix: "urn:uuid:"; - With curly braces: '{' and '}'; - With upper or lower case; - With or without hyphens. It is much faster than UUID.fromString(String) in JDK 8.
        Parameters:
        string - a UUID string
        Returns:
        a UUID
        Throws:
        InvalidUuidException - if invalid
      • toTimeBasedUuid

        public static UUID toTimeBasedUuid​(UUID uuid)
        Convert a time-ordered UUID to a time-based UUID.
        Parameters:
        uuid - a UUID
        Returns:
        another UUID
        Throws:
        IllegalArgumentException - if the input is not a time-ordered UUID
      • toTimeOrderedUuid

        public static UUID toTimeOrderedUuid​(UUID uuid)
        Convert a time-based UUID to a time-ordered UUID.
        Parameters:
        uuid - a UUID
        Returns:
        another UUID
        Throws:
        IllegalArgumentException - if the input is not a time-based UUID
      • toAndFromMsGuid

        public static UUID toAndFromMsGuid​(UUID uuid)
        Convert a UUID to and from a MS GUID. This method is only useful for MS SQL Server. It rearranges the most significant bytes from big-endian to little-endian, and vice-versa. The Microsoft GUID format stores the most significant bytes as little-endian, while the least significant bytes are stored as big-endian (network order).
        Parameters:
        uuid - a UUID
        Returns:
        another UUID