是否有一个用于使用日期构造cron表达式的java类?

时间:2010-08-19 10:12:51

标签: java cron

我需要根据表单中的用户输入构建一个cron表达式。所以我有一个日期选择器和一个选择框来选择频率。我可以很容易地从中构建一个cron表达式,但感觉就像我正在重新发明轮子。有没有合适的课程为我做这个?像

这样的方法
public String cron(String frequency, Calendar nextRunTime);

2 个答案:

答案 0 :(得分:0)

我认为石英中的Cron功能可以为您提供此功能,但您必须进行一些编程才能使其正常工作。 (http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html

如果我正确理解你的问题,那就是这样。

答案 1 :(得分:0)

我已经创建了一个使用Quartz Scheduler的类。请查看下面的课程及其用法

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;
    }
}
  

用法:

            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);

注意

我应该将ID CronExpressionHelper命名为而不是当前的ID,并且请查看默认值。您也可以为这些人编写getter / setter,但这是一个很好的起点。

博客:http://www.apachejava.blogspot.com