public class Today {
static int n=0;
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,9);//每天早上九点执行一次
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
Date date =c.getTime();//第一次执行定时任务的时间
final long period=60*1000;
Today today=new Today();
/*if(date.before(new Date())) {//防止刚运行,九点之后部署就执行
date = today.addDay(date, 1);
}*/
TimerTask task=new TimerTask() {
@Override
public void run() {
n=n+1;
System.out.println(11+"------->"+n);
}
};
Timer timer =new Timer();
timer.schedule(task, date, period);
}
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
}
timetask定时任务
public class Today {
static int n=0;
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,9);//每天早上九点执行一次
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
Date date =c.getTime();//第一次执行定时任务的时间
final long period=60*1000;//1分钟执行一次
Today today=new Today();
/*if(date.before(new Date())) {//防止刚运行,九点之后部署就执行
date = today.addDay(date, 1);
}*/
TimerTask task=new TimerTask() {
@Override
public void run() {
n=n+1;
System.out.println(11+"------->"+n);
}
};
Timer timer =new Timer();
timer.schedule(task, date, period);
}
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
}