在指定时间执行定时任务

public class TimerTest {
Timer timer;

public TimerTest){
    Date time = getTime();
    System.out.println("指定时间time=" + time);
    timer = new Timer();
    timer.schedule(new TimerTaskTest02(), time);
}

public Date getTime(){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    calendar.set(Calendar.MINUTE, 39);
    calendar.set(Calendar.SECOND, 00);
    Date time = calendar.getTime();
    
    return time;
}

public static void main(String[] args) {
    new TimerTest02();
}

}

public class TimerTaskTest02 extends TimerTask{

@Override
public void run() {
    System.out.println("指定时间执行线程任务...");
}

}

当时间到达11:39:00时就会执行该线程任务,当然大于该时间也会执行!!执行结果为:

你可能感兴趣的:(定时)