3

I need to set a quartz trigger for a job that includes a timezone. E.g. I need to set it for 27 August 2012 at 15:00 Europe/Amsterdam timezone. My server works on UTC.

I believe a SimpleTriggeris what I need, but I do not know how to set a timezone for it.

Any idea?

STW
  • 44,917
  • 17
  • 105
  • 161
checklist
  • 12,340
  • 15
  • 58
  • 102

3 Answers3

2

SimpleTrigger's fire time is based on the configured start time.

The start time is specified as a java.util.Date, which is really just time specified in milliseconds since Jan 1, 1970 00:00:00.000 GMT.

If, at Oct 23, 2010 8:13:45 EST your code executes new Date(), and sets the result as the start time, Quartz will store as the start time the value in milliseconds (1287839625000), and then the trigger will fire at Oct 23, 2010 8:13:45 EST regardless of which timezone the scheduler is running in.

If you use java.util.Calendar to define a date and time in a particular time zone, and then convert that to a Date, it will work exactly the same: If you have specified Oct 23, 2010 8:13:45 and the Calendar's time zone is EST then the resulting milliseconds value will again be 1287839625000. But if the timezone was PST (but the date time the same) the milliseconds value would be 1287850425000 (3 hours later).

Just make sure the system clock where Quartz is running has been correctly set and is maintained as UTC by the operating system.

Som Bhattacharyya
  • 3,972
  • 35
  • 54
  • yes. this can solve the problem. But it involves manually handling the timezone. I have looking for Quartz to handle such things. – checklist Sep 24 '12 at 14:25
  • i guess then you may consider using the cron trigger variant and pass the timezone to the crontrigger implemnation class's object. – Som Bhattacharyya Sep 24 '12 at 14:46
  • i guess then you may consider using the cron trigger variant and pass the timezone to the crontrigger implemnation class's object. Also please tell me why do you think u have to manually handle it here ... What is it that you are expecting from the quartz component. – Som Bhattacharyya Sep 24 '12 at 14:53
  • So u can use this kind of an expression for that , trigger = newTrigger() .withIdentity("trigger3", "group1") .withSchedule(weeklyOnDayAndHourAndMinute(DateBuilder.WEDNESDAY, 10, 42)) .forJob(myJobKey) .inTimeZone(TimeZone.getTimeZone("America/Los_Angeles")) .build(); Please also consult the documentation available at "http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06" it is pretty straightforward and should help. – Som Bhattacharyya Sep 25 '12 at 06:44
  • It seems you cannot explicitly specify the timezone for a Quartz [SimpleTrigger](http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-05.html). To do so, I believe you need to use [CronTrigger](http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-06.html). – Woodchuck Feb 16 '23 at 18:34
1

I had the same requirement.I was able to achieve this using following way.

  1. Convert user time to UTC utcTime =userTimetoUTCTime("09/28/2017 12:00:00 AM")
  2. Re convert UTC to server time zone and set it to simple trigger utcTimetoUserTime(utcTime)

    public static String userTimetoUTCTime(String userTime) {

        ZoneId zone = ZoneId.systemDefault();
    
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a").withZone(zone);
        ZonedDateTime utc = ZonedDateTime.parse(userTime, fmt).withZoneSameInstant(ZoneOffset.UTC);
    
        // Convert Local Time to UTC
        OffsetDateTime gmtTime = utc.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
        // Utc to local time
        // gmtTime.atZoneSameInstant(ZoneId.systemDefault()).toString();
        return gmtTime.toString();
    }
    

public static Date utcTimetoUserTime(String utcTime) { return new DateTime(utcTime).toDate(); }

Note : Can set time zone in CronTrigger but not in SimpleTrigger

gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • Note : Can set time zone in CronTrigger but not in SimpleTrigger - Can you elaborate this little more, any link to this in doc ? – shalin Apr 29 '19 at 06:03
  • 1
    time zone option is enabled only on cron trigger. plz refer the follwoing link to get some idea https://stackoverflow.com/questions/26403391/difference-between-cron-trigger-and-simple-trigger-in-quartz-scheduler – gihan-maduranga May 03 '19 at 05:47
0

Quartz's SimpleTrigger does not appear to provide a way to set the timezone for the trigger. You may need to use CronTrigger, which does take a timezone.

As alluded to in another answer above, SimpleTrigger appears to use the timezone of the System date where Quartz is running when creating a trigger. CronTrigger, on the other hand, will allow you to specify a different timezone, as needed, since it takes a timezone parameter.

The startAt method, used by SimpleTrigger, takes a java.util.Date (optionally via DateBuilder functions). But java.util.Date does not provide a way to set the timezone, only a way to get it.

The other answers and comments have good info. Just adding this background info which I found helpful while facing this same issue.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70