Skip to content

Instantly share code, notes, and snippets.

@LeeSanghoon
Last active May 29, 2023 19:35
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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);
}
@igal-getrailo
Copy link

you are only using 64 out of the 128 bit, no? so this is not really a unique id anymore.

@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