java代码生成流水号

public String getPackageId() {

String packageId = "";
DateFormat fmt = new SimpleDateFormat("yyyyMMdd");
String dateStr = fmt.format(new Date());
Integer dateInt = Integer.parseInt(dateStr);

OrderPackage op = orderPackageDao.findMaxById();
if (op == null) {
packageId = dateStr + "00001";

} else {

String id = op.getId();
String idSub = id.substring(0, 8.);
Integer idInt = Integer.parseInt(idSub);
if (dateInt > idInt) {

packageId = dateStr + "00001";

} else if (dateInt.equals(idInt)) {

String before = id.substring(0, 8.);
String numStr =id.substring(8,id.length());

Integer numInt = Integer.parseInt(numStr);
Integer addnumInt = numInt + 1;

Integer numLenght = addnumInt.toString().length();

Integer lenght = 5 - numLenght;

StringBuffer sb = new StringBuffer();
for (int i = 0; i < lenght; i++) {
sb.append("0");
}

String mid = sb.toString();
packageId = before + mid + addnumInt;
}
}
return packageId;
}

猜你喜欢

转载自keney-oak.iteye.com/blog/2249739