The class java.util.Timer
has exactly what you need:
First, set-up the task to be scheduled:
TimerTask task = new TimerTask() {
void run() {
//do the task
}
};
Second, schedule the task:
Date futureDate = ...///whenever you want
Timer timer = new Timer();
timer.schedule(task, futureDate);
Compute the delay between now and your target date, and call schedule()
with this delay as argument:
Date targetDate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").parse(dateAsString);
long delayInMillis = targetDate - System.currentTimeMillis();
scheduler.schedule(task, delayInMillis, TimeUnit.MILLISECONDS);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.