贴代码:
/** * 生成订单号 * yyyyMMddHHmmss 6位随机 4位用户id * @return */ public static String getGenerateOrderNo(Long userId){ String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); StringBuffer stringBuffer = new StringBuffer(userId.toString()); //id长度大于4 取后四位 if(stringBuffer.length()>4){ stringBuffer = new StringBuffer(stringBuffer.substring(stringBuffer.length()-4,stringBuffer.length())); } //长度小于4 补全4位 while (stringBuffer.length()<4){ stringBuffer.insert(0,"0"); } return time+RandomUtil.getNumber(6)+stringBuffer.toString(); }
算了一下,并发下id小于4位数,唯一性很强,超过4位数的id,20190929124569--126547--0034 假设某一毫秒同时要进来9999*