I run Tomcat with -Duser.timezone=UTC
. However Quartz scheduler 2.2.1 seems to run in Europe/Prague which is my OS timezone.
Is there a way to run Quartz in custom timezone or determine which timezone Quartz is using? If not, is there a way to determine OS timezone programatically?
Quartz by default will use the default system locale and timezone, and it is not programed to pick up the property user.timezone you are providing your app. Remember also that this is only applies to a CronTrigger and not a SimpleTrigger.
If you are using Spring for example:
<bean id="timeZone" class="java.util.TimeZone" factory-method="getTimeZone">
<constructor-arg value="GMT" />
</bean>
<bean id="yourTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="yourJob" />
<property name="cronExpression" value="0 0 0/1 * * ?" />
<property name="timeZone" ref="timeZone" />
</bean>
If you are using plain java:
Trigger yourTrigger = TriggerBuilder
.newTrigger()
.withIdentity("TRIGGER-ID", "TRIGGER-GROUP")
.withSchedule(CronScheduleBuilder
.cronSchedule("0 0 0/1 * * ?")
.inTimeZone(TimeZone.getTimeZone("GMT")))
).build();
If you are using the XML configuration file, e.g. the quartz-config.xml
from Example To Run Multiple Jobs In Quartz of mkyong, you can configure the timezone in the element time-zone
:
<schedule>
<job>
<name>JobA</name>
<group>GroupDummy</group>
<description>This is Job A</description>
<job-class>com.mkyong.quartz.JobA</job-class>
</job>
<trigger>
<cron>
<name>dummyTriggerNameA</name>
<job-name>JobA</job-name>
<job-group>GroupDummy</job-group>
<!-- It will run every 5 seconds -->
<cron-expression>0/5 * * * * ?</cron-expression>
<time-zone>UTC</time-zone>
</cron>
</trigger>
</schedule>
See also Java's java.util.TimeZone for to see the ID for several timezones.
You can call setTimeZone() to set the time zone of your choosing for anything in Quartz that inherits BaseCalendar.
Java's TimeZone class has a getDefault() which should aid in determining OS timezone programmatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With