生成流水号DEMO

/**
* @auth lengzj
* @param firstNo 前缀
* @param lastNo 已存在最大编号
* @param dateFormatStr 日期格式规则
* @param leng 末尾长度
* 例如:XS 201903 001
* @return
*/
public static String creatSerialNum(String firstNo,String lastNo,String dateFormatStr,int leng){
SimpleDateFormat format= new SimpleDateFormat(dateFormatStr);
String date=format.format(new Date());
/**
* 此处可以冲数据库中查询然后进行比较,如果为空默认为lastno如果部位空
* 讲取出的流水号作为temp,然后截取,累加返回。
*/
StringBuffer sb=new StringBuffer();
String temp=lastNo.substring(lastNo.length()-leng, lastNo.length());

int num = (int)Math.pow(10,leng)-1;

if(Integer.parseInt(temp)>=1&&Integer.parseInt(temp)<num){
temp=String.valueOf(Integer.parseInt(temp)+1);
}

String seat="";
for(int i=0;i<leng-temp.length();i++){
seat=seat+"0";
}
temp=seat+temp;

lastNo=firstNo+date+temp;
return lastNo;

}


public static void main(String[] args) {
String no=creatSerialNum("NNN","NNN201903200001","yyyyMMdd",4);
System.out.println("流水号"+'\n'+no);

}

posted on 2015-10-29 09:41  静以修身!  阅读(281)  评论(0编辑  收藏  举报

导航