ลองใช้วิธีนี้ ->
ขั้นแรกสร้างคลาส TimeTask ที่รันงานของคุณดูเหมือน:
public class CustomTask extends TimerTask  {
   public CustomTask(){
     //Constructor
   }
   public void run() {
       try {
         // Your task process
       } catch (Exception ex) {
           System.out.println("error running thread " + ex.getMessage());
       }
    }
}
จากนั้นในคลาสหลักคุณสร้างอินสแตนซ์ของภารกิจและรันเป็นระยะ ๆ ตามวันที่ที่ระบุ:
 public void runTask() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(
           Calendar.DAY_OF_WEEK,
           Calendar.MONDAY
        );
        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 40);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Timer time = new Timer(); // Instantiate Timer Object
        // Start running the task on Monday at 15:40:00, period is set to 8 hours
        // if you want to run the task immediately, set the 2nd parameter to 0
        time.schedule(new CustomTask(), calendar.getTime(), TimeUnit.HOURS.toMillis(8));
}
               
              
TimeUnitนำไปใช้ทั้งในและinitialDelayperiodทำงานทุก 24 ชั่วโมงจะจบลงด้วยการถูกโยนออกเมื่อเตะเวลาใน แต่TimeUnitของไม่ยอมให้คุณระบุเม็ดเล็กDAYSinitialDelay(ฉันคิดว่าการใช้งาน ScheduledExecutorService ภายในจะแปลงDAYSเป็นนาโนวินาทีอยู่ดี)