Java 生成随机订单号

Java 生成随机订单号

/**

  • 根据时间生成随机订单号
    */
    public class GenerateNum {
    // 全局自增数
    private static int count = 0;

    // 每毫秒秒最多生成多少订单(最好是像9999这种准备进位的值)
    private static final int total = 999;

    // 格式化的时间字符串
    private static final SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmssSSS”);

    // 获取当前时间年月日时分秒毫秒字符串
    private static String getNowDateStr() {
    return sdf.format(new Date());
    }

    // 记录上一次的时间,用来判断是否需要递增全局数
    private static String now = null;

    /*

    • 生成一个订单号
      */
      public synchronized String GenerateOrder() {
      String datastr = getNowDateStr();
      if (datastr.equals(now)) {
      count++;// 自增
      } else {
      count = 1;
      now = datastr;
      }
      int countInteger = String.valueOf(total).length() - String.valueOf(count).length();// 算补位
      String bu = “”;// 补字符串
      for (int i = 0; i < countInteger; i++) {
      bu += “0”;
      }
      bu += String.valueOf(count);
      if (count >= total) {
      count = 0;
      }
      return datastr + bu;
      }

    /**

    • 生成单据号

    • @param type 单据类型 C_FID3判断

    • @param con con

    • @param mp 用于数据库查询需要

    • @return 单据号格式eg:191209001
      */
      public static String InvoiceNo(String type,Connection con) {
      String warranty = “000”;
      String sql=“select C_id from bas_batchno where C_BATCHNOTYPE =’”+type+"’ and to_days(D_CTIME) = to_days(now()) order by d_ctime desc limit 1;";
      sql=DBUtil.getSelect(con, sql, null);
      if(StringUtil.isNullOrEmpty(sql)) {
      sql=“000”;
      }else {
      int length = sq

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值