效率还可以,生成100万数据大概在1秒多左右,不想写说明了,直接粘贴~
public static void main(String[] args) {
System.out.println("开始时间" + new SimpleDateFormat("YYYY-MM-dd HH:mm:ss:SSS").format(new Date()));
List<String> result = new ArrayList<>();
for (int i = 0; i < 1000000; i++){
result.add(randomOrderCode());
}
System.out.println("结束时间" + new SimpleDateFormat("YYYY-MM-dd HH:mm:ss:SSS").format(new Date()));
System.out.println("总生成数量:" + result.size());
String code = randomOrderCode();
System.out.println("订单号:" + code);
}
// 生成订单号方法
public static String randomOrderCode(){
String fix = "DH"; // 前缀
Integer len = 18; // 总长度,必须超过fix.length+dateran.length的长度
// 获取时间字符串
SimpleDateFormat dmDate = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String dateran = dmDate.format(date);
// 生成随机数
Random ran = new Random();
StringBuilder str = new StringBuilder();
for (int i = 0; i < len - fix.length() - dateran.length(); i++) {
str.append(ran.nextInt(10));
}
// 拼接字符串
return fix + dateran + str;
}