动态生成16位不重复随机数、随机创建2位ID

动态生成16位不重复随机数、随机创建2位ID

/**
1. * 动态生成16位不重复随机数
 * 
 * @return
 */
public synchronized static String generate16() {
    StringBuffer stringBuffer = new StringBuffer();
    String date = DatetimeUtil.getDateyyyyMMddNone();
    String seconds = DatetimeUtil.getDateHHmmssNone();
    stringBuffer.append(date).append(generate2()).append(seconds);
    return stringBuffer.toString();
}

public static String getDateyyyyMMddNone() {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String fd = sdf.format(date);
    return fd;
}

public static String getDateHHmmssNone() {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
    String fd = sdf.format(date);
    return fd;
}

2.
/**
 * 随机创建2位ID
 * 
 * @return String 2位随机数ID
 */
public synchronized static String generate2() {
    Random rad = new Random();
    String result = rad.nextInt(100) + "";
    if (result.length() == 1) {
        result = "0" + result;
    }
    return result;
}

 

posted @ 2019-04-02 09:43 A点点圈圈A 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(动态生成16位不重复随机数、随机创建2位ID)