Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Quartz default timezone

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?

like image 950
Fandic Avatar asked Oct 23 '13 14:10

Fandic


Video Answer


3 Answers

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();
like image 191
eadjei Avatar answered Oct 12 '22 02:10

eadjei


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.

like image 32
Paul Vargas Avatar answered Oct 12 '22 00:10

Paul Vargas


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.

like image 2
Robert Gannon Avatar answered Oct 12 '22 02:10

Robert Gannon