Skip to content

Instantly share code, notes, and snippets.

@LeeSanghoon
Last active May 29, 2023 19:35
Show Gist options
  • Save LeeSanghoon/5811136 to your computer and use it in GitHub Desktop.
Save LeeSanghoon/5811136 to your computer and use it in GitHub Desktop.
Generate short UUID
import java.nio.ByteBuffer;
import java.util.UUID;
/**
* Generate short UUID (13 characters)
*
* @return short UUID
*/
public static String shortUUID() {
UUID uuid = UUID.randomUUID();
long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong();
return Long.toString(l, Character.MAX_RADIX);
}
@filosganga
Copy link

It is only less unique the UUID because the 128 bit is not uique as well.

@loucadufault
Copy link

loucadufault commented Nov 20, 2020

As a one-liner:

String shortUUID = Long.toString(ByteBuffer.wrap(UUID.randomUUID().toString().getBytes()).getLong(), Character.MAX_RADIX);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment