Java recurring task, date issue

I am trying to set a scheduled task in java to run once a day.
The problem is that it only works on the first day.
Any idea y?
thank

log.info("Schdualing midnight task");
    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();

    date.set(Calendar.HOUR_OF_DAY, 23);
    date.set(Calendar.MINUTE, 30);
    date.set(Calendar.SECOND, 0);

    timer.schedule(new EndOfDayRatesTimerTask(new MidnightQuotesEvent()),
            date.getTime());
+3
source share
2 answers

Use scheduleAtFixedRate () instead . For instance,

TimerTask task = new EndOfDayRatesTimerTask(new MidnightQuotesEvent());
timer.scheduleAtFixedRate(task, date.getTime(), TimeUnit.DAYS.toMillis(1));
+5
source

You are using the single line version schedule(). There is a version that takes an additional parameter to indicate a delay between subsequent executions.

0
source

Source: https://habr.com/ru/post/1758994/


All Articles