How to start a process at a specific time in java?

I have one application that receives the start and end time from the user and starts a specific process at (start time) runs to (end time), for example, I use the TimerTask utility if it only starts the process from the current time and runs to (end time), I can not set the start time, how can I compare the user time (start time) and the system time in java

//my sample  program
import java.sql.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Main {
  public static void main(String[] argv) throws Exception {

     int numberOfMillisecondsInTheFuture=1000;

//    start time= dynamically set by user
//    end time =dynamically set by user


    Date timeToRun = new Date(System.currentTimeMillis() + numberOfMillisecondsInTheFuture);//here is the problem.
    Timer timer = new Timer();

    timer.schedule(new TimerTask() {
      public void run() {

      //System.out.println("doing");
      //doing some task not related to this question
      }
    }, timeToRun);
  }
+5
source share
1 answer

if that helps. How can I run my TimerTask every day at 2 p.m. You may need to make some changes to suit your needs.

+3
source

All Articles