容易出现的问题:
近三个月的数据:比如说现在是2017年1月那么就要追述到2016年的额12月与2016年的额11月
所以动态的时间获取如下:
Date date1=new Date();//当前的时间
Date date2=DateUtil.addDate(-1, (short)2);//利用DateUtil将当前时间减去一个月
Date date3=DateUtil.addDate(-2, (short)2);//将当前的时间减去两个月
Integer count1 = salsemanSeedService.selectMonthCountBySalesmanId( (date1.getYear()+1900), (date1.getMonth()+1));//当前的的年与月 传进去进行查询
Integer count2 = salsemanSeedService.selectMonthCountBySalesmanId( (date2.getYear()+1900), (date2.getMonth()+1));//减去一个月之后的年与月 传进去进行查询
Integer count3 = salsemanSeedService.selectMonthCountBySalesmanId((date3.getYear()+1900), (date3.getMonth()+1));//减去两个月之后的年与月 传进去进行查询
sql语句如下:
<select id="selectMonthCountBySalesmanId" resultType="java.lang.Integer" >
SELECT count(id) count
FROM tbl_su_business
WHERE DATE_FORMAT(create_date_time,'%Y') = #{year} //在数据库中转换年
AND DATE_FORMAT(create_date_time,'%m') = #{month} //在数据库中转换月
AND salesman_id = #{salesmanId}
AND invalid = 0
</select>```
addDate的工具类如下:
/**
* 时间加法
*
* @param number
* 天数 或月数 的数值
* @param unit
* 天 或 月 的单位 unit 1为天 2为月
* @return
*/
public static Date addDate(int number, short unit) {
Calendar c = Calendar.getInstance();
if (1 == unit) {
c.add(Calendar.DAY_OF_MONTH, number);
}
else {
c.add(Calendar.MONTH, number);
}
return c.getTime();
}