Search code examples
javacronschedulequartzhijri

Schedule job using non Gregorian calendar cron expression


Is there a way to schedule cron jobs using the Islamic Calendar? Ideally:

CronExpression cronExpression = new CronExpression("0 0 0 1 * ? *", CalendarEnum.ISLAMIC);

I'm struggling to find the smallest clue on Java Quartz documentation (1 mention on QA section) and I've inspected the source code and found out that the default calendar (found it in CronExpression.java) is the Gregorian. Is there a way to achieve this by using the inclusion/exclusion org.quartz.Calendar? I think not, right?

Does anyone have knowledge of how to extend/implement different variations of cron expression to cover e.g. an Islamic calendar? Or alternative open source libraries (hosted on mvn central repo)? Or any approach at all, to achieve an abstracted way of expressing recurrence (e.g. repeat every 1st day of each Islamic month)?

Temporarily, I'm thinking of manually binding multiple org.quartz.Trigger dates to the job (converted from Islamic to Gregorian). But, this is not optimal since a single cron expression may result in a dozen or hundred of dates.

Sorry, for the not so technical question, but the topic seems to be uncovered and information sparse.


Solution

  • Since the Islamic Calendar and the Gregorian Calendar are not aligned and the Islamic Months have a different length then the Gregorian Months, it is rather tricky. I would suggest performing a cron implementation of the following style (Linux Cron Style):

    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
    # |  |  |  |  |
    # *  *  *  *  *   command to be executed
      x  y  *  *  *   IslamicMonthTestCmd && command
    

    The idea is to do daily a test at x hours and y minutes to check if you are on the first of the Islamic Month. This is done by IslamicMonthTestCmd which you still have to write yourself. If this test passes then you execute command.

    Writing this IslamicMonthTestCmd is not straightforward as the Islamic Calendar, in contrary to the Gregorian, is heavily influenced by Men. The months depend on the visibility of the new moon by religious authorities and can, therefore, vary by a day or two, clearly influenced by the people, weather and various other disturbances. The current Islamic year is 1440 AH. In the Gregorian calendar, 1440 AH runs from approximately 11 September 2018 to 30 August 2019. [source: Wikipedia]