javaspringtimezonescheduled-tasks

Customizing time zone with TaskScheduler.getClock()


From the documentation of CronTrigger(String expression, ZoneId zoneId):

Note that such explicit time zone customization is usually not necessary, using TaskScheduler.getClock() instead.

What is that supposed to mean? You can't set the ZoneId of a Clock.


Solution

  • What was probably meant:

    Spring's "standard" implementation of TaskScheduler is ThreadPoolTaskScheduler, which has setClock(Clock). You can call this method by implementing ThreadPoolTaskSchedulerCustomizer:

    @Override
    public void customize(final ThreadPoolTaskScheduler taskScheduler) {
        taskScheduler.setClock(Clock.system(zoneId));
    }