我的数据库当中的一个字段是:要求必须为当前日期然后再加上四个流水号,如:200602170001,当前日期这个好取,但是怎样我们会得到像0002这样的字符串值呢?声明一下这个字段为字符串值:0001进行数值转换就成了1而不是0001,当然再加1只能得到2,大家告诉我怎样解决就会得到:0002

解决方案 »

  1.   

    /**
     * @param num 
     * @param len 长度
     * @return
     * @throws NumberFormatException
     */
    public static String inc(String num, int len) throws NumberFormatException {
    int number = Integer.parseInt(num);
    String strNumber = new Integer(++number).toString();
    while (strNumber.length() < len) {
    strNumber = "0" + strNumber;
    }
    return strNumber;
    }
      

  2.   

    int n = 1;//或其他数值String str = "000";
    str += n;
    str = str.substring(str.length() - 4);