Search code examples
cronquartz-schedulerbatch-processingcronexpression

Quartz cron trigger for every 2 weeks and 2 times a day ( 6 AM and 6PM)


I want to run a quartz job for every 2 weeks at morning 6'o clock and evening 6'o clock . how to achieve this .please advice.

I tried to schedule using below cron expression

01 01 1 1-0/14 01 ? *

but the next fire times are as below.

  1. Sunday, January 1, 2017 1:01 AM
  2. Sunday, January 15, 2017 1:01 AM
  3. Sunday, January 29, 2017 1:01 AM
  4. Monday, January 1, 2018 1:01 AM
  5. Monday, January 15, 2018 1:01 AM

Solution

  • There are some kinds of date triggers cron is good at. Every N days is often not one of them. For instance, if you use something like this:

    0 0 6,18 */14 * ?
    

    You might get something like this:

    1. Tuesday, March 29, 2016 6:00 AM
    2. Tuesday, March 29, 2016 6:00 PM
    3. Friday, April 1, 2016 6:00 AM
    4. Friday, April 1, 2016 6:00 PM
    5. Friday, April 15, 2016 6:00 AM
    6. Friday, April 15, 2016 6:00 PM

    As you can see, the 29th, followed by the 1st -- not exactly fourteen days apart.

    It's useful to have something that lets you play with the expression and see the resulting dates. CronMaker can do that, if that's helpful.

    However, since you say you are using Quartz, it supports other kinds of triggers, like DateIntervalTrigger that might be better suited for what you're looking for?