package cn.huge.base.common.utils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.nio.ByteBuffer;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.Random;
|
import java.util.UUID;
|
|
/**
|
* @title: ID生成工具
|
* @description: ID生成工具
|
* @company: hugeinfo
|
* @author: liyj
|
* @time: 2021-11-05 16:51:48
|
* @version: 1.0.0
|
*/
|
public class IdUtils {
|
|
/**
|
* 新建编号
|
*/
|
public static String NEW_ID = "new";
|
|
/**
|
* 时间Id统计常量
|
*/
|
private static int COUNT_NUM = 1000;
|
|
/**
|
* 时间Id统计常量,常量前缀
|
*/
|
private static int SIGN_COUNT_NUM = 1000;
|
|
/**
|
* 时间Id统计常量,常量后缀
|
*/
|
private static int COUNT_NUM_SIGN = 1000;
|
|
/**
|
* UUID随机字符串,有"-"
|
* @return String
|
*/
|
public static String uuid()
|
{
|
return UUID.randomUUID().toString();
|
}
|
|
/**
|
* UUID随机字符串,无"-"
|
* @return String
|
*/
|
public static String uuid2()
|
{
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
}
|
|
/**
|
* UUID随机字符串
|
* @return String
|
*/
|
public static String uuid3()
|
{
|
long val = ByteBuffer.wrap(uuid2().getBytes()).getLong();
|
return Long.toString(val, 36);
|
}
|
|
/**
|
* 获取带有的时间Id,后四位随机数
|
* @return String
|
*/
|
public synchronized static String getTimeRandomId(){
|
String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
|
String randomNum = getRandomNum(4);
|
String timeId = str.replaceAll("-","").replaceAll(":","").replaceAll(" ","") + randomNum;
|
timeId = timeId.substring(2,timeId.length());
|
return timeId;
|
}
|
|
//随机产生N位数字随机数
|
public static String getRandomNum(int length) { //length表示生成字符串的长度
|
String base = "0123456789";
|
Random random = new Random();
|
StringBuffer sb = new StringBuffer();
|
for (int i = 0; i < length; i++) {
|
int number = random.nextInt(base.length());
|
sb.append(base.charAt(number));
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 判断是否为新增对象编号
|
*/
|
public static Boolean checkNewId(String id){
|
if (StringUtils.isEmpty(id) || NEW_ID.equals(id)){
|
return true;
|
}else{
|
return false;
|
}
|
}
|
|
/**
|
* 获取时间Id,后四位常量
|
* @return String
|
*/
|
public synchronized static String getNewTimeId(){
|
String timeId = getNowTime() + (COUNT_NUM++);
|
if(COUNT_NUM > 99999){
|
COUNT_NUM = 10000;
|
}
|
timeId=timeId.substring(2, timeId.length());
|
return timeId;
|
}
|
|
/**
|
* 获取时间Id,后四位常量
|
* @return String
|
*/
|
public synchronized static String getNowTime(){
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
String nowTime = sdf.format(new Date());
|
return nowTime;
|
}
|
}
|
/**
|
* -------------------_ooOoo_-------------------
|
* ------------------o8888888o------------------
|
* ------------------88" . "88------------------
|
* ------------------(| -_- |)------------------
|
* ------------------O\ = /O------------------
|
* ---------------____/`---'\____---------------
|
* -------------.' \\| |// `.-------------
|
* ------------/ \\||| : |||// \------------
|
* -----------/ _||||| -:- |||||- \-----------
|
* -----------| | \\\ - /// | |-----------
|
* -----------| \_| ''\---/'' | |-----------
|
* -----------\ .-\__ `-` ___/-. /-----------
|
* ---------___`. .' /--.--\ `. . __----------
|
* ------."" '< `.___\_<|>_/___.' >'"".-------
|
* -----| | : `- \`.;`\ _ /`;.`/ - ` : | |-----
|
* -----\ \ `-. \_ __\ /__ _/ .-` / /-----
|
* ======`-.____`-.___\_____/___.-`____.-'======
|
* -------------------`=---='
|
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
* ---------佛祖保佑---hugeinfo---永无BUG----------
|
*/
|