xiangpei
2024-07-23 95f0a8b4d82a859f2018c9d77e1a8a3a38b2d523
ycl-server/src/main/java/com/ycl/utils/uuid/IdUtils.java
@@ -1,5 +1,9 @@
package com.ycl.utils.uuid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
 * ID生成器工具类
 * 
@@ -7,6 +11,9 @@
 */
public class IdUtils
{
    private final static SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
    /**
     * 获取随机UUID
     * 
@@ -46,4 +53,29 @@
    {
        return UUID.fastUUID().toString(true);
    }
    /**
     * 获取当前时间+随机数的编号
     *
     * @param numberNum 时间后生成几位数字,默认5
     * @return 编号
     */
    public static String timeAddRandomNO(Integer numberNum)
    {
        if (numberNum == null || numberNum == 0 || numberNum < 3) {
            numberNum = 5;
        }
        Date now = new Date();
        String timeString = FORMAT.format(now);
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        builder.append(timeString);
        for (int i = 0; i < numberNum; i++) {
            // 生成一个0到9之间的随机数(包括0和9)
            builder.append(random.nextInt(10));
        }
        return builder.toString();
    }
}