Pu Zhibing
2025-02-28 8baab7959e8efd5be4c0276b7759c2b677eeb7df
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.ruoyi.common.core.utils;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
 
public class OrderCodeUtil {
 
    public static String getOrderCode(String code){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDate = now.format(formatter);
 
        // 四位随机数
        Random random = new Random();
        int randomNumber = random.nextInt(9999); // 生成0-9998之间的随机数
        String formattedRandomNumber = String.format("%04d", randomNumber); // 补齐到4位
 
        // 拼接日期和随机数
        return code+formattedDate + formattedRandomNumber;
    }
    public static void main(String[] args) {
        // 测试方法
        System.out.println(getOrderCode("DH"));
    }
}