Parsing Crontab Syntax With Java

I wrote a small piece of code I wanted to share: I wanted to parse/interpret crontab style syntax in java and found out that there is not a single simple way to do this available right now. So I wrote a very basic parser.

Allows you to parse syntax like: */15,12 7-11,13-17 * * 1-5

Download the source

Update: It seems this code is now to be found in certain IBM products (with my permission).

  1. Was thinking of doing something similar

  2. Huch, genau das hab ich gesucht und bin jetzt eher durch Zufall darauf gestossen. Sehr geil! Danke 🙂

  3. I think there might be a small bug in your code. I believe the months parse range starts at 1 not 0.

    Month = parseRangeParam(params[3], MONTHSPERYEAR, 0); // 0 = january

    should be

    Month = parseRangeParam(params[3], MONTHSPERYEAR, 1); // 1 = january

    The rest is great!

  4. You are right – there was a bug in there and it had to do with the “month-handling”:
    The problem was that when you called mayRunAt(Calendar cal) and set the month to december (=11) but wrote the cron line with, for example,
    “1 7 * 12 1-5” (12 => december) it would not run.

    I uploaded the new code – please let me know in case it doesn’t work for you.

  5. Thank you very much, that’s what I needed.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>