package com.mytimer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class TimerTest extends TimerTask implements ServletContextListener {
private int num = 1;
public void run() {
System.out.println("定时器调用了" + num + " 次");
if(new Date().getHours() == 2){
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
num++;
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("服务器停止");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("服务器启动");
Timer t = new Timer();
t.schedule(new TimerTest(), 10000, 1000);
}
}