Labros
Labros

Reputation: 11

Setting quartz schedule with startdate and timezone

I have the following method to schedule a job to fired at different timezone and start date.

If the server's timezone is Asia/Taipei, and the schedule is set to the following inputs:

triggerName="testTrigger",
jobName="testJob", 
group="testGroup", 
startDate = 2014-12-03 18:00:00
timezoneString = "Austrailia/Melbourne"
cronExp = 0 0/1 * 1/1 * ? *

When should be the firing time?

  public void schedule(String triggerName, String jobName, String group, Date startDate,       String timezoneString, String cronExp){
    try {
        JobDetail jobDetail = new JobDetail(jobName, group, MessageJob.class);

        TimeZone timeZone = TimeZone.getTimeZone(timezoneString);
        Date startDate = startDate;

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startDate);
        cal1.setTimeZone(timeZone);
        startDate = cal1.getTime();

        CronTrigger cronTrigger = new  CronTrigger(triggerName, group, jobName, jobGroup, startDate, null, cronExp, timeZone);
        scheduler.start();

    } catch (ParseException e) {
        System.out.println("ParseException issues " + e.getMessage());
    }catch(IllegalArgumentException e){
        System.out.println("IllegalArgumentException issues " + e.getMessage());
    }
}

Upvotes: 0

Views: 2239

Answers (1)

Jan Moravec
Jan Moravec

Reputation: 1880

If you explicitely set a different timezone in a Quartz cron trigger, then the trigger will fire according to the trigger configuration in the specified time zone. I.e. your cron trigger expresion will be evaluated using the Australia/Melbourne timezone.

To prove this, I created a sample cron trigger with the Australia/Melbourne time zone in QuartzDesk. My local timezone is CET (Central European Time). In the GUI you can clearly see that the trigger's next fire time as returned by Quartz is 2014-12-04 14:00:00 (in the local CET time zone).

Australia/Melbourne trigger in QuartzDesk

Now when you convert this time to the Australia/Melbourne timezone (I use this online converter), I get 2014-12-04 00:00:00 which is the expected trigger time:

Midnight in Australia/Melbourne timezone = 14:00:00 in CET timezone

As for when your cron expression (0 0/1 * 1/1 * ? *) fires, it fires every minute on every hour (01:00:00, 01:01:00,...) in the Australia/Melbourne timezone. Now considering that Taipei is Australia/Melbourne + 3 hours, then the fire times in your timezone will be 21:01:00, 21:02:00, ...

Upvotes: 1

Related Questions