java生成编号(无序和有序)

供小白来使用吧 ,可以借鉴可以学习,这些我都实际测试过,可以直接使用,在使用这些 我尽量都加上注解,这样方便大家根据自己的需求改动。当然有什么不好的地方和需要改的可以联系我(第二个我看到别人来写的,如有原创可和我说,我贴地址)

1、无序生成编号(时间+编号)

public static String getDateAndUUID() {
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy");//获取时间格式 时间的完整格式为yyyyMMddHHmmss
        String cdtSequen =     tempDate.format(new java.util.Date());// 获取当前时间
        String s = UUID.randomUUID().toString().toUpperCase();//获取UUID UUID为唯一码,
        String rtnstr = cdtSequen+s.substring(0, 4);//时间+uuid拼装      截图UUID的前四位,具体根据自己需求来截取
        return rtnstr;
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        System.out.println(getDateAndUUID());

    }

2 有序生成(时间+编号)

 public static String generateNumber() throws SQLException {
        String Orderno = null;

        String maxOrderno=“select top 1 xxx from xxxx  where xxx= 0  order by id desc ”//查询最新编号 有更好的方法联系我
     
        System.out.println("maxOrderno=" + maxOrderno);
        SimpleDateFormat format = new SimpleDateFormat("yyyy"); // 获取时间格式 时间的完整格式为yyyyMMddHHmmss
        String uid_pfix = format.format(new Date()); // 组合流水号前一部分,NO+时间字符串,如:NO20160126
        System.out.println("time=" + format.format(new Date()));
        if (maxOrderno != null && maxOrderno.contains(uid_pfix)) {  //
            String uid_end =

           maxOrderno.substring(4, 8); // 截取字符串最后四位,结果:0001,根据自己的位数来截,不然会报出超出范围
            System.out.println("uid_end=" + uid_end);
            int endNum = Integer.parseInt(uid_end); // 把String类型的0001转化为int类型的1
            System.out.println("endNum=" + endNum);
            int tmpNum = 10000 + endNum + 1; //编号来加
            System.out.println("tmpNum=" + tmpNum);
            Orderno = uid_pfix + subStr("" + tmpNum, 1);//最后的拼装
        } else {
            Orderno = uid_pfix + "0001";
        }
        return Orderno;
        
    }

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值