位数不足前面补零:
String.format("%06d",19);
转换后类型是String,保留6位,不足前面补0;d表示后面参数是整型
- // 当前年月日,20090818格式
- String currentFormatDate = Util.getCurrentFormatDate();
-
- // 找到最大的编号
- List list = baseDAO.findByNamedQuery("findMaxSNFromDistribute");
-
- if (list != null && !list.isEmpty()) {
- String maxNumber = (String) list.get(0); // 最大的编号
- String date = maxNumber.substring(6, maxNumber.length() - 3); // 年月日串
- String number = maxNumber.substring(14, maxNumber.length()); // 后三位数字
- // 后三位数字+1 , 长度为3,不足自动补0
- number = String.format("%03d", Integer.valueOf(number) + 1);
- if (date.equals(currentFormatDate)) {
- return "CBDis-" + currentFormatDate + number;
- } else {
- return "CBDis-" + currentFormatDate + "001";
- }
-
- }
-
-
-
- // Util.java工具类
-
- public class Util {
-
- public static String getCurrentFormatDate(){
- Date date = new Date(System.currentTimeMillis());
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- String currentDate = dateFormat.format(date);
- StringBuffer buf = new StringBuffer("");
- buf.append(currentDate.substring(0, 4)).append(
- currentDate.substring(5, 7)).append(
- currentDate.substring(8, 10));
- return buf.toString();
- }
- }
评论