Skip to content

Instantly share code, notes, and snippets.

@biezhi
Created May 29, 2018
Embed
What would you like to do?
Java 短UUID生成
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
public class UUIDUtil {
public static String shortUUID() {
UUID uuid = UUID.randomUUID();
return shortUUID(uuid);
}
protected static String shortUUID(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.allocate(16);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return Base64.getEncoder().withoutPadding().encodeToString(byteBuffer.array())
.replaceAll("/", "_")
.replaceAll("\\+", "-");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment