【AgileTC】_Calendar类实现指定时间戳
主要依靠Calendar工具类的相关方法来实现;
1.依据获取当前时间减一天、一月、一年实现获取当前时间前几天时间戳的方法:
1
2
3
4
5
6
7
8
9/**
* 获取几天前的时间
* 输入为负数表示几天前,为正数表示几天后。
*/
private Date getLastTime(int index) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH,index);
return calendar.getTime();
}2.判断获取24小时前或7天前的时间:
1
2
3
4
5
6
7
8if (!StringUtils.isEmpty(timeLimit)) {
Date endTime = new Date();
if (timeLimit.equals(ONEDAY)) {
Date beginTime = getLastTime(-1);
}else if (timeLimit.equals(ONEWEEK)) {
Date beginTime = getLastTime(-7);
}
}3.在mapper.xml配置文件中配置时间戳比较条件,mysql数据库中的TIMESTAMP类型字段数据可以直接与java中的Date类型时间戳数据通过上述sql语句进行比较:
1
2
3
4
5
6<if test="beginTime != null">
and gmt_created >= #{beginTime,jdbcType=TIMESTAMP}
</if>
<if test="endTime != null">
and gmt_created <= #{endTime,jdbcType=TIMESTAMP}
</if>