java根据年月日 加四位编号 自动生成订单号
作者:互联网
public String oderCode(){
String orderBgegin="DD";
String orderCode = "";//表中存在的最大订单号
Calendar cal = Calendar.getInstance();
int day1 = cal.get(Calendar.DATE);
String day = String.valueOf(cal.get(Calendar.DATE)).format("%02d", day1);//当前日
int month1 = cal.get(Calendar.MONTH) + 1;
String month = String.valueOf(cal.get(Calendar.MONTH) + 1).format("%02d", month1);//当前月
String year = String.valueOf(cal.get(Calendar.YEAR));//当前年
String oldday = "";//最大订单号中日
String oldmonth = "";//最大订单号中日月
String oldyear = "";//最大订单号中日年
String oldnum = "";
//查出数据库中最大的订单号
ComponentOrder componentOrder=componentOrderMapper.selectList(new QueryWrapper<ComponentOrder>().select("order_code").last("order by create_time desc")).get(0);
//截取订单号中年月日
orderCode = componentOrder.getOrderCode();
oldyear = orderCode.substring(2,6);
oldmonth = orderCode.substring(6,8);
oldday = orderCode.substring(8,10);
oldnum = orderCode.substring(10);
//判断是否当天最新订单,进行拼接
if(month.equals(oldmonth) && day.equals(oldday) && year.equals(oldyear)){
AtomicInteger val = new AtomicInteger(Integer.valueOf(oldnum));
int nextVal = val.incrementAndGet();
String i = String.format("%04d", nextVal);
orderCode = orderBgegin+oldyear+oldmonth+day+i;
}else{
orderCode = orderBgegin+year+month+day+"0000";
}
return orderCode;
}
标签:java,String,orderCode,get,订单号,cal,Calendar,年月日 来源: https://blog.csdn.net/Shengzhiying/article/details/119319805