背景:调用递归进行返回一个想要的值
问题:返回的值总是会被覆盖 ,使用debug进行断点排查,发现首次的值是正确的的但是会重新执行return语句 值被覆盖掉。
原因:之前的调用也会逐一出栈且反回一个值,多次的调用就会被多次的覆盖
解决办法:每次返回的变量都接收变量值
代码大体意思就是 根据 起始日期 加 间隔天数 再加 数据库中配置的相应范围内的多余天数 得出最终的日期
有问题的代码,核心点就是lastDate这个变量
public Date getLastDates(Date createTime,Integer backlogCycle){
Calendar ca = Calendar.getInstance();
ca.setTime(backlogCreateTime);//这里设置基础日期
ca.add(Calendar.DATE,backlogCycle);//dayNum是天数
//得到新的日期
Date lastDate = ca.getTime();
List<DateCon> dateCons=myDateService.selectDateConList(createTime,lastDate);
int days=0;
if (dateCons!=null&&dateCons.size()>0){
days=dateCons.size();
}
if (days!=0){
getLastDates(lastDate,days);
}
return lastDate;
}
解决: 变量每次都接收更新的 lastDate=getLastDates(lastDate,days);
public Date getLastDates(Date createTime,Integer backlogCycle){
Calendar ca = Calendar.getInstance();
ca.setTime(backlogCreateTime);//这里设置基础日期
ca.add(Calendar.DATE,backlogCycle);//dayNum是天数
//得到新的日期
Date lastDate = ca.getTime();
List<DateCon> dateCons=myDateService.selectDateConList(createTime,lastDate);
int days=0;
if (dateCons!=null&&dateCons.size()>0){
days=dateCons.size();
}
if (days!=0){
lastDate=getLastDates(lastDate,days);
}
return lastDate;
}