生成16位id

This commit is contained in:
2023-11-11 17:16:27 +08:00
parent ee3095d02a
commit be89f49d74
2 changed files with 37 additions and 1 deletions

View File

@@ -4,6 +4,8 @@ import com.google.common.collect.Sets;
import com.jsowell.common.util.DateUtils;
import com.jsowell.common.util.RandomUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
/**
@@ -48,6 +50,28 @@ public class IdUtils {
return UUID.fastUUID().toString(true);
}
/**
* 通过UUID生成16位唯一订单号
*/
public static String get16UUID(){
// 1.开头两位,标识业务代码或机器代码(可变参数)
String machineId = "89";
// 2.中间四位整数,标识日期
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
String dayTime = sdf.format(new Date());
// 3.生成uuid的hashCode值
int hashCode = UUID.randomUUID().toString().hashCode();
// 4.可能为负数
if(hashCode < 0){
hashCode = -hashCode;
}
// 5.算法处理: 0-代表前面补充0; 10-代表长度为10; d-代表参数为正数型
String value = machineId + dayTime + String.format("%010d", hashCode);
System.out.println(value);
return value;
}
/**
* 生成交易流水号
* @param pileSn 桩编号 例如32010600019236