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

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

猜你喜欢

转载自www.cnblogs.com/goatherd/p/11363081.html
今日推荐