JAVA 用时间生成主键ID
/** * 生成主键(19位数字) * 主键生成方式,年月日时分秒毫秒的时间戳 例如:1810311557430000845 */ public static synchronized Long getUUIDTOLongNew(){ try { TimeUnit.NANOSECONDS.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); } String msg=""; Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSSSSSS"); //1810311557430000845 // msg = sdf.format(date).substring(2); return Long.parseLong(sdf.format(date)); }
/**
* 生成主键(17位数字)
* 主键生成方式,年月日时分秒毫秒的时间戳 例如:180910094204299
*/
public static synchronized Long getUUIDTOLong(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String msg="";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
msg = sdf.format(date).substring(2);
return Long.parseLong(msg);
}
你所拥抱的并不总是拥抱你