function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ravikant kediaRavikant kedia 

How to generate cron expression . ?

Hi all,
           I want to write cron expression for schedule job or perform some operation.
    
1)-Daily
             a)- Every week day
             b)- Every 1 days or Every 2 days so on.

2)-Weekly
             a)-  Recurs every 1 week(s) on or  Recurs every 2 week(s) on so on.
             b) - Days like sunday ,monday and so on.
3)- Monthly
              a)-On day (1-31) of every (1 or, 2,or,3 so on)   month(s).
              b)-On the (1-4, Last) day(sunday-saturday) of every  (1,2,so on )  month(s).
4)- Yearly
               a)- On every (Janury-December) (1-31)
               b)- On the (1-4, Last) day(sunday-saturday) of (Janury-December)
             

       
Best Answer chosen by Ravikant kedia
Gaurav NirwalGaurav Nirwal
The apex code is 
custom utility class that converts a date object to a cron expression:-

public class CronUtil {

private final Date mDate;
private final Calendar mCal;
private final String mSeconds = “0”;
private final String mDaysOfWeek = “?”;

private String mMins;
private String mHours;
private String mDaysOfMonth;
private String mMonths;
private String mYears;

public CronUtil(Date pDate) {
this.mDate = pDate;
mCal = Calendar.getInstance();
this.generateCronExpression();
}

private void generateCronExpression() {
mCal.setTime(mDate);

String hours = String.valueOf(mCal.get(Calendar.HOUR_OF_DAY));
this.mHours = hours;

String mins = String.valueOf(mCal.get(Calendar.MINUTE));
this.mMins = mins;

String days = String.valueOf(mCal.get(Calendar.DAY_OF_MONTH));
this.mDaysOfMonth = days;

String months = new java.text.SimpleDateFormat(“MM”).format(mCal.getTime());
this.mMonths = months;

String years = String.valueOf(mCal.get(Calendar.YEAR));
this.mYears = years;

}

public Date getDate() {
return mDate;
}

public String getSeconds() {
return mSeconds;
}

public String getMins() {
return mMins;
}

public String getDaysOfWeek() {
return mDaysOfWeek;
}

public String getHours() {
return mHours;
}

public String getDaysOfMonth() {
return mDaysOfMonth;
}

public String getMonths() {
return mMonths;
}

public String getYears() {
return mYears;
}

}


All Answers

KevinPKevinP
Checkout this site http://www.cronmaker.com (http://www.cronmaker.com" target="_blank) to generate your cron expressions.
Ravikant kediaRavikant kedia
I want apex code for generate cron expressions.

Gaurav NirwalGaurav Nirwal
The apex code is 
custom utility class that converts a date object to a cron expression:-

public class CronUtil {

private final Date mDate;
private final Calendar mCal;
private final String mSeconds = “0”;
private final String mDaysOfWeek = “?”;

private String mMins;
private String mHours;
private String mDaysOfMonth;
private String mMonths;
private String mYears;

public CronUtil(Date pDate) {
this.mDate = pDate;
mCal = Calendar.getInstance();
this.generateCronExpression();
}

private void generateCronExpression() {
mCal.setTime(mDate);

String hours = String.valueOf(mCal.get(Calendar.HOUR_OF_DAY));
this.mHours = hours;

String mins = String.valueOf(mCal.get(Calendar.MINUTE));
this.mMins = mins;

String days = String.valueOf(mCal.get(Calendar.DAY_OF_MONTH));
this.mDaysOfMonth = days;

String months = new java.text.SimpleDateFormat(“MM”).format(mCal.getTime());
this.mMonths = months;

String years = String.valueOf(mCal.get(Calendar.YEAR));
this.mYears = years;

}

public Date getDate() {
return mDate;
}

public String getSeconds() {
return mSeconds;
}

public String getMins() {
return mMins;
}

public String getDaysOfWeek() {
return mDaysOfWeek;
}

public String getHours() {
return mHours;
}

public String getDaysOfMonth() {
return mDaysOfMonth;
}

public String getMonths() {
return mMonths;
}

public String getYears() {
return mYears;
}

}


This was selected as the best answer
Gaurav NirwalGaurav Nirwal
Now lets see how to use this class


public void generateCronExpression(Date date) {

try {

SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String dt = dateFormat.format(date);

Date cronDate = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).parse(dt);

CronUtility calHelper = new CronUtility(cronDate);
String cron = calHelper.getSecondsDuration() + ” ” +
calHelper.getMinsDuration() + ” ” +
calHelper.getHoursDuration() + ” ” +
calHelper.getDaysOfMonthDuration() + ” ” +
calHelper.getMonthsDuration() + ” ” +
calHelper.getDaysOfWeekDuration() + ” ” +
calHelper.getYearsDuration();
logger.debug(“Cron Expression ” + cron);

}

Gaurav NirwalGaurav Nirwal
You can use this code to generate the icon expression 
the above two blocks of code in a single program
 
thanks 

Please select as best answer