import java.util.Date;
import java.util.TimerTask;
public class CustomTimerTask extends TimerTask {
public Date setDate = null;
public CustomTimerTask() {
}
public CustomTimerTask(Date setDate) {
this.setDate = setDate;
}
@Override
public void run() {
if (setDate != null) {
Date date = new Date(System.currentTimeMillis());
if (date.toLocaleString().equals(setDate.toLocaleString())) {
System.out.println(date.toLocaleString());
}
}
}
}
public class StartTask {
public static void main(String[] args){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timer timer=new Timer();
//第一个任务的时间
Date setDate=format.parse("2012-7-10 15:51:05");
timer.schedule(new CustomTimerTask(setDate),setDate);
//第二个任务的时间
setDate=format.parse("2012-7-10 15:51:25");
timer.schedule(new CustomTimerTask(setDate),setDate);
}
}
java TimerTask一般说明
最新推荐文章于 2024-04-30 11:09:46 发布