Java Quartz default timezone

16,549

Solution 1

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.

Solution 2

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();

Solution 3

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.

Share:
16,549
Fandic
Author by

Fandic

Updated on June 09, 2022

Comments

  • Fandic
    Fandic about 2 years

    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?

  • Fandic
    Fandic over 10 years
    Maybe Iam not able to explain my problem properly, my appologies. My app is running in different timezone than the quartz. I need to set timezone of whole quartz, not of triggers. TimeZone.getDefault() says app is in UTC, but quartz triggers in Europe/Prague
  • Fandic
    Fandic over 10 years
    I set triggers programmatically. Also i need to set timeZone of quartz itself
  • Fandic
    Fandic over 10 years
    In other words: Quartz seems to ingnore -Duser.timezone=UTC VM argument
  • Robert Gannon
    Robert Gannon over 10 years
    @Fandic, if you could expound on your architecture/setup/scenario in the original question that would be useful. Regarding setting the timezone for all of quartz, perhaps you could use the custom key/value pairs offered as part of the Main Config options? Also, you could probably use your -Duser.timezone=UTC JVM arg and enforce it by virtue of TimeZone.setDefault()?
  • Oleg
    Oleg almost 5 years
    CronTriggerBean was removed in spring 4.1