- applicationContext-quertz.xml的配置,开启job任务注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd ">
<task:annotation-driven/>
</beans>
@Component(Timmer.serialVersionUID + "")
public class Timmer implements Serializable{
private static final long serialVersionUID = -2499873556819765772L;
private static Log logger = LogFactory.getLog(Timmer.class);
@Scheduled(cron = "0 0 */3 * * ?")
public void timer() throws Exception {
try{
}catch (Exception e) {
logger.debug(e.getMessage());
}
}
}
- 由于不止一个这样的timmer所以,当第一个timmer到第二个timmer的时候会存在一定的误差,所以就需要考虑到凌晨十二点不是整点触发进入程序的,所以这个误差的范围需要考虑,又因为每天凌晨跑的应该是昨天一天的数据而且误差范围不能大于零点之后的三个小时因为凌晨三点执行的是当天的数据,所以说误差范围就是当前时间小于凌晨三点就跑昨天的数据。
我们用Calendar类进行处理,Date不适用于改变时间
首先获取当前的时间
1.获取Calendar的对象 Calendar c = Calendar.getInstance()
2.获取当前时间的毫秒数用来比较当天凌晨三点的毫秒数,判断时间范围用的
long currentMills = c.getTimeInMillis()
3.//c.getTime()
计算一天的数据为 当天的头一天的23:59:59到当天的明天的00:00:00
4.获取头一天的23点59分59秒,并格式化时间为“YYYY-MM-dd HH:mm:ss”
String sdTime = ReportUitls.format(TimeUtil.preDay(c.getTime()))
TimeUtil.preDay()的函数为:
public Date preDay(Date date){
Calendar c1 = Calendar.getInstance()
c1.setTime(date)
c1.add(c1.DATE,-1)
c1.set(c.get(c1.YEAR),c1.get(c1.MONTH),c1.get(c1.DAY_OF_MONTH),23,59,59)
//get(int field) 返回给定日历字段的值。
return new Date(c.getTimeInMillis())
}
5.获取头一天的00点00分00秒,并格式化时间为“YYYY-MM-dd HH:mm:ss”
String edTime = ReportUitls.format(TimeUtil.backDay(c.getTime()))
TimeUtil.backDay()的函数为:
public Date backDay(Date date){
Calendar c1 = Calendar.getInstance()
c1.setTime(date)
c1.add(c1.DATE,1)
c1.set(c.get(c1.YEAR),c1.get(c1.MONTH),c1.get(c1.DAY_OF_MONTH),00,00,00)
//get(int field) 返回给定日历字段的值。
return new Date(c.getTimeInMillis())
}
6.或指定的当天的凌晨三点的Calendar对象
Calendar threeCal = Calendar.getInstance()
threeCal.set(threeCal.get(threeCal .YEAR),threeCal .get(threeCal .MONTH),threeCal .get(threeCal.DAY_OF_MONTH),03,00,00)
@Scheduled(cron = "0 0 */3 * * ?")
public void timer() throws Exception {
try{
Calendar c = Calendar.getInstance()
String sdTime = ReportUitls.format(TimeUtil.preDay(c.getTime()))
String edTime = ReportUitls.format(TimeUtil.backDay(c.getTime()))
Calendar threeCal = Calendar.getInstance()
threeCal.set(threeCal.get(threeCal.YEAR),threeCal.get(threeCal.MONTH),threeCal.get(threeCal.DAY_OF_MONTH),03,00,00)
if(c.getTimeInMillis()<threeCal.getTimeInMillis()){
c.add(Calendar.DATE, -1)
sdTime = ReportUitls.format(TimeUtil.preDay(c.getTime()))
edTime = ReportUitls.format(TimeUtil.backDay(c.getTime()))
}
//在这处理service层的数据将sdTime和edTime传入,处理当天一天的数据
}catch (Exception e) {
logger.debug(e.getMessage())
System.out.println(e.getMessage())
}
}