java定时任务

java定时任务实现方法:

java定时任务
 1 public class TimingTask {
 2     private static int count = 0;
 3     private static SpiderService service = null;
 4       public static void startTask(int hour,int minute,int second) {
 5             TimerTask task = new TimerTask() {
 6                 @Override
 7                 public void run() {
 8                     service = new SpiderService();
 9                     service.service();
10                     ++count;
11                     System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
12                 }
13             };
14 
15             //设置执行时间
16             Calendar calendar = Calendar.getInstance();
17             int year = calendar.get(Calendar.YEAR);
18             int month = calendar.get(Calendar.MONTH);
19             int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
20             //定制每天的21:09:00执行,
21             calendar.set(year, month, day, hour, minute, second);
22             Date date = calendar.getTime();
23             Timer timer = new Timer();
24             System.out.println(date);
25             
26            // int period = 2 * 1000;
27             //每天的date时刻执行task,每隔2秒重复执行
28            // timer.schedule(task, date, period);
29             //每天的date时刻执行task, 仅执行一次
30             timer.schedule(task, date);
31         }
java定时任务

调用方法:

1     public static void main(String[] args) {
2                 //下面是两个任务
3             TimingTask.startTask(11, 40, 00);
4             TimingTask.startTask(11, 45, 00);
5         }

java定时任务

上一篇:CRM2011与CRM2013获取ID


下一篇:为/Date(1332919782070)/转时间2013-09-23