Java自动生成订单号

java生成订单号/出库单号/入库单号等

java自动生成唯一出库单号

/**
 * @author: 
 * @date: 2019/12/27
 * @time: 16:54
 * @description:
 */
public class TimeNumberUtils {
    public static void main(String[] args) {
	  //测试方法略..自己去测了哈
 	//结果: ===>20200407133313000001       
    }

    
private static int sequence = 0;
private static int length = 6;

public static synchronized String getLocalTrmSeqNum() {
    sequence = sequence >= 999999 ? 1 : sequence + 1;
    String datetime = new SimpleDateFormat("yyyyMMddHHmmss")
            .format(new Date());
    String s = Integer.toString(sequence);
    return datetime +addLeftZero(s, length);
    }

    /**
     * 左填0
     * @author shijing
     * 2018年6月29日下午1:24:32
     * @param s
     * @param length
     * @return
     */
public static String addLeftZero(String s, int length) {
        // StringBuilder sb=new StringBuilder();
        int old = s.length();
        if (length > old) {
            char[] c = new char[length];
            char[] x = s.toCharArray();
            if (x.length > length) {
                throw new IllegalArgumentException(
                        "Numeric value is larger than intended length: " + s
                                + " LEN " + length);
            }
            int lim = c.length - x.length;
            for (int i = 0; i < lim; i++) {
                c[i] = '0';
            }
            System.arraycopy(x, 0, c, lim, x.length);
            return new String(c);
        }
        return s.substring(0, length);
    }
}

理论上应该可以支撑支持每秒十万个订单 - , - 如果不对的地方还请大佬多多关照

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值