You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
40 lines
1.0 KiB
package com.hai.common.utils;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
/**
|
|
* 生成唯一标识
|
|
*
|
|
* @author
|
|
* @createDate
|
|
*/
|
|
public class IdentifyUtil {
|
|
public static void main(String[] args) {
|
|
//调用生成id方法
|
|
System.out.println(getGuid());
|
|
}
|
|
|
|
/**
|
|
* 20位的数字id
|
|
*/
|
|
public static int Guid = 100;
|
|
|
|
public static String getGuid() {
|
|
IdentifyUtil.Guid += 1;
|
|
long now = System.currentTimeMillis();
|
|
//获取4位年份数字
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
|
|
//获取时间戳
|
|
String time = dateFormat.format(now);
|
|
String info = now + "";
|
|
//获取三位随机数
|
|
//int ran=(int) ((Math.random()*9+1)*100);
|
|
//要是一段时间内的数据连过大会有重复的情况,所以做以下修改
|
|
int ran = 0;
|
|
if (IdentifyUtil.Guid > 999) {
|
|
IdentifyUtil.Guid = 100;
|
|
}
|
|
ran = IdentifyUtil.Guid;
|
|
return time + info.substring(2, info.length()) + ran;
|
|
}
|
|
}
|
|
|