java随机生成订单号

发布于:2023-01-10 ⋅ 阅读:(269) ⋅ 点赞:(0)
package com.aaa.utils;

import org.springframework.stereotype.Component;

/**
     * @项目名:ppp
     * @类名: DateUtil.java
     * @包名 com.aaa.util
     * @类描述: 随机生成订单号
     * @创建人: 王大饼
     * @创建时间: 2022年7月11日 下午14:39:56
     */
@Component
    public class DateUtil {

        public static String getCurrentDateStr() {
            // TODO Auto-generated method stub
            String number = Math.floor(((Math.random()*1000000+1000001)))+"";
            String num=number.substring(0,number.indexOf("."));
            return num;
        }
}

调用随机订单号

        String num=DateUtil.getCurrentDateStr();

下面是以日期加时分秒加随机数生成订单号

package com.aaa.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class TestMYsql {
    public static void main(String[] args) {

        TestMYsql testMYsql = new TestMYsql();
        System.out.println(testMYsql.getTimeRand());
    }

    private SimpleDateFormat sdf = null;
    public String getTimeRand()
    {
        StringBuffer buf = new StringBuffer();
        buf.append(getTimeStamp());
        Random r = new Random();
        for (int i = 0; i < 3; i++) {
            buf.append(r.nextInt(10));
        }
        return buf.toString();
    }
    public String getDate() {
        this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return this.sdf.format(new Date());
    }
    public String getTimeStamp() {
        this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return this.sdf.format(new Date());
    }
}

网站公告

今日签到

点亮在社区的每一天
去签到