简体   繁体   中英

How run task in future in java at a particular date

I have have to call a method in future so i found some example Link are there Link 1 link 2

But I have to run it ONE TIME only. at Date and Time : 11-03-2014 10:15:20 (dd-MM-yyyy HH:MM:SS)

I how do it??

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM