首页 > 编程语言 > 详细

java自定义编码或流水号处理

时间:2019-08-16 12:56:38      阅读:125      评论:0      收藏:0      [点我收藏+]

1.前面自动补位,方式一:

    public static void main(String[] args) {
        int i = 12;
        NumberFormat nf = NumberFormat.getIntegerInstance();
        nf.setGroupingUsed(false);//设置是否使用分组
        nf.setMaximumIntegerDigits(4);//设置最大整数位数
        nf.setMinimumIntegerDigits(4);//设置最小整数位数    
        String num = nf.format(i);
        System.out.println("补位后:"+num);
    }

  输出结果:补位后:0012

2.前面自动补位,方式二:

    public static void main(String[] args) {
        int i = 89;      
        //0:代表前面补充0;4:代表长度为4;d:代表参数为正数型      
        String str = String.format("%04d", i);      
        System.out.println(str);
    }

输出结果:0089

3.全是数字的流水号,自增1之后补位:

    public static void main(String[] args) {
        String liuShuiHao = "0020190815";
        Integer intHao = Integer.parseInt(liuShuiHao);
        intHao++;
        String strHao = intHao.toString();
        while (strHao.length() < liuShuiHao.length())strHao = "0" + strHao;
        System.out.println("流水号:"+strHao);
    }

输出结果:流水号:0020190816

 

java自定义编码或流水号处理

原文:https://www.cnblogs.com/goatherd/p/11363081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!