根据时间创建每天重置流水号

第一次写博客,有讲解的不好的地方还请大家多多包涵哈。。。


首先,根据时间生成,我们先要格式化一下时间格式

SimpleDateFormat dmfot = new SimpleDateFormat("yyyyMMdd");
//截取当前时间作为流水号
String preCode = dmfot.format(new Date());

然后我们进行数据库查询,今天最大的流水号是多少?

//获取最大编号
String maxCode = getMaxCode(date);
查询语句如下:
select Max(code)
from table_name
where 
declare_time >= STR_TO_DATE(#{date},'%Y-%m-%d')

这里我们重置一下时间格式,获取当前时间零点

     /*** 返回当前时间零点* @return*/public static String getCurrentDay() {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Calendar c = Calendar.getInstance();c.setTime(new Date()); c.set(Calendar.HOUR_OF_DAY, 0);  c.set(Calendar.MINUTE, 0);  c.set(Calendar.SECOND, 0);Date date = c.getTime();String currentDay = format.format(date);return currentDay;}

查询之后进行判断,如果查出的数据为空,证明今天没有生成流水号,我们直接set"001";例如  20191017-001

//设置流水号格式,根据实际情况设置
DecimalFormat num=new DecimalFormat("000");
if(maxCode != null && !"null".equals(maxCode)){//对流水号截取后三位String code = maxCode.substring(maxCode.length()-3,maxCode.length());//例如后三位为002,通过以下步骤,将会变为003int count = Integer.valueOf(code)+1;String number = num.format(count);return preCode + "-" + number;
}else {return preCode + "-" + "001";
}

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部