Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類
本文實(shí)例講述了Java實(shí)現(xiàn)產(chǎn)生隨機(jī)字符串主鍵的UUID工具類。分享給大家供大家參考,具體如下:
package com.gcloud.common;
import java.net.InetAddress;
import java.util.UUID;
/**
* uuid工具類
* Created by charlin on 2017/9/9.
*/
public class UUIDUtil {
private String sep = "";
private static int IP = 0;
private static short counter = (short) 0;
private static int JVM = (int) (System.currentTimeMillis() >>> 8);
static {
try {
IP = toInt(InetAddress.getLocalHost().getAddress());
} catch (Exception e) {
}
}
private static UUIDUtil instance = new UUIDUtil();
public static UUIDUtil getInstance() {
return instance;
}
/**
* byte轉(zhuǎn)int
*
* @param bytes
* @return
*/
public static int toInt(byte[] bytes) {
int result = 0;
for (int i = 0; i < 4; i++) {
result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
}
return result;
}
protected String format(int intval) {
String formatted = Integer.toHexString(intval);
StringBuffer buf = new StringBuffer("00000000");
buf.replace(8 - formatted.length(), 8, formatted);
return buf.toString();
}
protected String format(short shortval) {
String formatted = Integer.toHexString(shortval);
StringBuffer buf = new StringBuffer("0000");
buf.replace(4 - formatted.length(), 4, formatted);
return buf.toString();
}
protected int getJVM() {
return JVM;
}
protected synchronized short getCount() {
if (counter < 0) {
counter = 0;
}
return counter++;
}
protected int getIP() {
return IP;
}
protected short getHiTime() {
return (short) (System.currentTimeMillis() >>> 32);
}
protected int getLoTime() {
return (int) System.currentTimeMillis();
}
public String generate() {
return new StringBuffer(36).append(format(getIP())).append(sep).append(
format(getJVM())).append(sep).append(format(getHiTime()))
.append(sep).append(format(getLoTime())).append(sep).append(
format(getCount())).toString();
}
// 得到一個(gè)序號(hào)
public static String getUUID() {
String s = UUID.randomUUID().toString();
return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
}
/**
* 一次得到多個(gè)序號(hào)
*
* @param number int 需要獲得的序號(hào)數(shù)量
* @return String[] 序號(hào)數(shù)組
*/
public static String[] getUUID(int number) {
if (number < 1) {
return null;
}
String[] ss = new String[number];
for (int i = 0; i < ss.length; i++) {
ss[i] = getUUID();
}
return ss;
}
public static void main(String[] str) {
System.out.println("腳本之家測(cè)試結(jié)果:");
// 得到一個(gè)序號(hào)
System.out.println(getUUID());
System.out.println("----------------------------");
// 一次得到多個(gè)序號(hào)
String[] UUID_s = getUUID(10);
for (int i = 0; i < UUID_s.length; i++) {
System.out.println(UUID_s[i]);
}
}
}
運(yùn)行結(jié)果:

PS:這里再為大家提供幾款功能類似的在線工具供大家參考:
在線隨機(jī)數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
在線隨機(jī)字符/隨機(jī)密碼生成工具:
http://tools.jb51.net/aideddesign/rnd_password
高強(qiáng)度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java如何實(shí)現(xiàn)長(zhǎng)圖文生成的示例代碼
這篇文章主要介紹了Java如何實(shí)現(xiàn)長(zhǎng)圖文生成的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Spring Cloud Gateway實(shí)現(xiàn)灰度發(fā)布方案
灰度發(fā)布是在微服務(wù)中的表現(xiàn)為同一服務(wù)同時(shí)上線不同版本,讓一部分用戶使用新版本來(lái)驗(yàn)證新特性,如果驗(yàn)證沒(méi)有問(wèn)題,則將所有用戶都遷移到新版本上,本文就來(lái)介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2023-12-12
如何解決maven搭建一直處于running:..狀態(tài)問(wèn)題
在使用Maven搭建項(xiàng)目時(shí),有時(shí)會(huì)遇到一直處于加載狀態(tài)的情況,通過(guò)修改設(shè)置可以解決這個(gè)問(wèn)題,具體步驟為:1. 打開File->Settings->Build, Execution, Deployment->Maven->running,然后在VMOptions中填寫"-DarchetypeCatalog=internal"2024-09-09
Java中的拷貝數(shù)組CopyOnWriteArrayList詳解
這篇文章主要介紹了Java中的拷貝數(shù)組CopyOnWriteArrayList詳解,ArrayList和LinkedList都不是線程安全的,如果需要線程安全的List,可以使用synchronizedList來(lái)生成一個(gè)同步list,但是這個(gè)同步list的方法都是通過(guò)synchronized修飾來(lái)保證同步的,需要的朋友可以參考下2023-12-12
簡(jiǎn)單實(shí)現(xiàn)Servlet文件下載功能
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)Servlet文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
如何使用IntelliJ IDEA中的Live Templates自定義代碼模板
在IntelliJ IDEA中,通過(guò)使用LiveTemplates功能,可以實(shí)現(xiàn)快速編碼和自定義代碼模板,例如,輸入“main”可以自動(dòng)補(bǔ)全主函數(shù)結(jié)構(gòu),“sout”可以補(bǔ)全輸出語(yǔ)句,用戶可以通過(guò)設(shè)置中的LiveTemplates選項(xiàng)查看和定義快捷模板,支持使用分組管理和參數(shù)化模板內(nèi)容,適應(yīng)復(fù)雜的編碼需求2024-11-11
java+testng+selenium的自動(dòng)化測(cè)試實(shí)例
這篇文章主要介紹了java+testng+selenium的自動(dòng)化測(cè)試實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

