Mark Chorley
Mark Chorley

Reputation: 2107

Is there a java class for constructing a cron expression using a date?

I need to build a cron expression based on user input from a form. So I have a datepicker and a select box to choose the frequency. I can build a cron expression from this easily enough, but it feels like I am reinventing the wheel. Is there a suitable class to do this for me? Something with a method like

public String cron(String frequency, Calendar nextRunTime);

Upvotes: 3

Views: 4870

Answers (2)

AZ_
AZ_

Reputation: 21899

I have made a class to work with Quartz Scheduler. Please find the class below and also its Usage

package com.az.ws.client.utility;

import java.util.Calendar;
import java.util.Date;


/**
 * @author AZ
 * 
 */

public final class CronCalendarHelper_ {

    private final Date mStartDate;
    private final Calendar mStartCal;
    private final Date mEndDate;
    private final Calendar mEndCal;
    private final String mSecondsDuration = "0";
    private final String mMinsDuration = "0/30";
    private final String mDaysOfWeekDuration = "?";

    private String mHoursDuration;
    private String mDaysOfMonthDuration;
    private String mMonthsDuration;
    private String mYearsDuration;

    public CronCalendarHelper_(Date pStartDate, Date pEndDate) {
        this.mStartDate = pStartDate;
        mStartCal = Calendar.getInstance();

        this.mEndDate = pEndDate;
        mEndCal = Calendar.getInstance();

        this.init();
    }

    private void init() {
        mStartCal.setTime(mStartDate);
        mEndCal.setTime(mEndDate);

        String h1 = String.valueOf(mStartCal.get(Calendar.HOUR_OF_DAY));
        String h2 = String.valueOf(mEndCal.get(Calendar.HOUR_OF_DAY));

        this.mHoursDuration = h1 + "-" + h2;

        String d1 = String.valueOf(mStartCal.get(Calendar.DAY_OF_MONTH));
        String d2 = String.valueOf(mEndCal.get(Calendar.DAY_OF_MONTH));

        this.mDaysOfMonthDuration = d1 + "-" + d2;

        // First month is January which is 0, inconsistent Java Date API
        String m1 = new java.text.SimpleDateFormat("MM").format(mEndCal.getTime());
        String m2 = new java.text.SimpleDateFormat("MM").format(mStartCal.getTime());

        if (Integer.valueOf(m2) - Integer.valueOf(m1) > 0) {
            this.mMonthsDuration = m1 + "-" + m2;
        } else {
            this.mMonthsDuration = m1;
        }

        if (mEndCal.get(Calendar.YEAR) - mStartCal.get(Calendar.YEAR) > 0) {
            String y1 = String.valueOf(mStartCal.get(Calendar.YEAR));
            String y2 = String.valueOf(mEndCal.get(Calendar.YEAR));

            this.mYearsDuration = y1 + "-" + y2;
        } else {
            this.mYearsDuration = String.valueOf(mStartCal.get(Calendar.YEAR));
        }

    }

    public Date getDate() {
        return mStartDate;
    }

    public String getSecondsDuration() {
        return mSecondsDuration;
    }

    public String getMinsDuration() {
        return mMinsDuration;
    }

    public String getDaysOfWeekDuration() {
        return mDaysOfWeekDuration;
    }

    public String getHoursDuration() {
        return mHoursDuration;
    }

    public String getDaysOfMonthDuration() {
        return mDaysOfMonthDuration;
    }

    public String getMonthsDuration() {
        return mMonthsDuration;
    }

    public String getYearsDuration() {
        return mYearsDuration;
    }
}

Usage:

            String startDateStr = "2013-10-19 09:00:00.0";
            String endDateStr = "2013-10-26 23:00:00.0";

            Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
            Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);

            CronCalendarHelper_ calHelper = new CronCalendarHelper_(startDate, endDate);
            String cron = calHelper.getSecondsDuration() + " " + calHelper.getMinsDuration() + " " + calHelper.getHoursDuration() + " "
                    + calHelper.getDaysOfMonthDuration() + " " + calHelper.getMonthsDuration() + " " + calHelper.getDaysOfWeekDuration() + " "
                    + calHelper.getYearsDuration();
            System.out.println("Injecting Cron Expression " + cron);

Note

I should name id CronExpressionHelper rather than the current one and also please see for default values. You can also write getter/setter for those but it's a good starting point.

Blog: http://www.apachejava.blogspot.com

Upvotes: 0

Kurt Du Bois
Kurt Du Bois

Reputation: 7655

I think the Cron-functionality in quartz could provide this for you, but you do have to do some programming to make it work. (http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html)

This is if I understand your question correctly.

Upvotes: 1

Related Questions