方法一:使用UUID
public static String getUUID() { String replaceUUID = UUID.randomUUID().toString().replace("-", ""); return replaceUUID; }
方法二:用时间(精确到毫秒)+随机数
public static synchronized String getOrderNum() { //时间(精确到毫秒) DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); String localDate = LocalDateTime.now().format(ofPattern); //3位随机数 String randomNumeric = RandomStringUtils.randomNumeric(3); String orderNum = localDate + randomNumeric; log.info("订单号:{}", orderNum); return orderNum; }
来源地址:https://blog.csdn.net/weixin_52293201/article/details/131438641