generate java cron expression

bean class:

com.cst.klocwork.service.cron Package; 

public class TaskScheduleModel {   
    
    
    / ** 
        * Type of the selected job:   
        * 1 -> day   
        * 2 -> Monthly   
        * 3 -> week 
        * 4 -> intervals (every 2 hours, every 30 minutes) 
        * /   
    Integer jobType;   
      
    / days of the week ** * /   
    Integer [] dayOfWeeks;   
      
    / days of the month ** * /   
    Integer [] dayOfMonths;   
      
    / sec ** * /   
    SECOND Integer;   
      
    / min ** * /   
    Integer minute;   
      
    / * time ** /   
    Integer hour;      
  
    public getJobType Integer () {   
        return jobType;   
    }  
  
    public void setJobType(Integer jobType) {  
        this.jobType = jobType;  
    }  
      
    public Integer[] getDayOfWeeks() {  
        return dayOfWeeks;  
    }  
  
    public void setDayOfWeeks(Integer[] dayOfWeeks) {  
        this.dayOfWeeks = dayOfWeeks;  
    }     
  
    public Integer[] getDayOfMonths() {  
        return dayOfMonths;  
    }  
  
    public void setDayOfMonths(Integer[] dayOfMonths) {  
        this.dayOfMonths = dayOfMonths;  
    }  
  
    public Integer getSecond() {  
        return second;  
    }  
  
    public void setSecond(Integer second) {  
        this.second = second;  
    }  
  
    public Integer getMinute() {  
        return minute;  
    }  
  
    public void setMinute(Integer minute) {  
        this.minute = minute;  
    }  
  
    public Integer getHour() {  
        return hour;  
    }  
  
    public void setHour(Integer hour) {  
        this.hour = hour;  
    }  
      
      
}  

  util class:

/ **   
 * @ClassName: CronUtil   
 * @Description: Cron expression tools class   
 * currently supports three common cron expression 
 * some point every day of execution 1. Example:? 121 212 * * Denotes 12 every day 12 minutes and 12 seconds to perform 
 * 2 days of the week to perform Example:? 121 212 * 1,2,3 weekly week 1 week 2 week 3, 12:12:12 execution 
 * 3. monthly days of execution Example:? 121,212 monthly 1,21,13 * denotes 1 21 13 12:12:12 execution 
 * @author   
 * @date   
 *   
 * /     
public class CronUtil {     
        
        
      
    / **   
     *    
     * summary: expression construct Cron   
     * @ param taskScheduleModel   
     * @ return String   
     * /     
    public static String createCronExpression (taskScheduleModel taskScheduleModel) {     
        the StringBuffer cronExp the StringBuffer new new = ( "");    
          
        IF (taskScheduleModel.getJobType == null ()) {   
            System.out.println ( "execute cycle is not configured"); // not configured to perform periodic   
        }   
          
        ! IF (= taskScheduleModel.getSecond null ()   
                ! = null && taskScheduleModel.getMinute ()   
                ! = null && taskScheduleModel.getHour ()) {     
            // sec     
            . cronExp.append (taskScheduleModel.getSecond ()) the append ( "");     
            // sub     
            cronExp.append (taskScheduleModel.getMinute ().) append ( " ");     
            // hours     
            cronExp.append (taskScheduleModel.getHour ()) the append (." ");     
                
            // daily    
            if(taskScheduleModel.getJobType().intValue() == 1){    
                cronExp.append ( "*"); // Day   
                cronExp.append ( "*"); // month   
                cronExp.append ( "?"); // weeks   
            }   
                
            // a weekly     
            else if (taskScheduleModel.getJobType () .intValue () == 3) {     
                // month for the first few days     
                cronExp.append () "?";     
                // month     
                cronExp.append ( "*");     
                // weeks     
                Integer [] weeks = taskScheduleModel.getDayOfWeeks ( );     
                for (int I = 0; I <weeks.length; I ++) {     
                    IF (I == 0) {     
                        cronExp.append (weeks [I]);    
                    } else{    
                        cronExp.append(",").append(weeks[i]);    
                    }    
                }     
                    
            }     
                
            // monthly     
            else if (. TaskScheduleModel.getJobType () intValue     () == 2) { 
                the days of the month //     
                Integer [] = taskScheduleModel.getDayOfMonths Days ();     
                for (int I = 0 ; I <days.length; I ++) {     
                    IF (I == 0) {     
                        cronExp.append (Days [I]);     
                    } else{    
                        cronExp.append(",").append(days[i]);    
                    }    
                }     
                // month     
                cronExp.append ( "*");     
                // circumferential     
                cronExp.append ( "? ");     
            }     
                
        }     
        the else {   
            while //; System.out.println ( "minutes or seconds, or when the configuration parameter is not") minutes or seconds, or the parameter is not configured   
        }   
        return cronExp.toString ();     
    }     
        
    / **   
     *    
     * summary: the detailed description of the generation plan   
     * @ param taskScheduleModel   
     * @ return String   
     * /     
    public static String createDescription (TaskScheduleModel taskScheduleModel) {     
        the StringBuffer description = new StringBuffer ( "");     
        // program execution start time     
// startTime = taskScheduleModel.getScheduleStartTime a Date ();     
            
        ! IF (null = taskScheduleModel.getSecond ()   
                && = null! taskScheduleModel.getMinute ()   
                && null! = taskScheduleModel.getHour ()) {    
            // per day    
            if(taskScheduleModel.getJobType().intValue() == 1){    
                description.append("每天");    
                description.append(taskScheduleModel.getHour()).append("时");    
                description.append(taskScheduleModel.getMinute()).append("分");    
                description.append(taskScheduleModel.getSecond()).append("秒");    
                description.append("执行");    
            }    
                
            //按每周    
            else if(taskScheduleModel.getJobType().intValue() == 3){    
                if(taskScheduleModel.getDayOfWeeks() != null && taskScheduleModel.getDayOfWeeks().length > 0) {    
                    String days = "";  
                    for(int i : taskScheduleModel.getDayOfWeeks()) {  
                        days += "周" + i;  
                    }  
                    description.append("每周的").append(days).append(" ");    
                }    
                if (null != taskScheduleModel.getSecond()  
                        && null != taskScheduleModel.getMinute()  
                        && null != taskScheduleModel.getHour()) {     
                    description.append(",");     
                    description.append(taskScheduleModel.getHour()).append("时");    
                    description.append(taskScheduleModel.getMinute()).append("分");    
                    description.append(taskScheduleModel.getSecond()).append("秒");   
                }    
                description.append("执行");    
            }    
                
            //按每月    
            else if(taskScheduleModel.getJobType().intValue() == 2){    
                //选择月份    
                if(taskScheduleModel.getDayOfMonths() != null && taskScheduleModel.getDayOfMonths().length > 0) {    
                    String days = "";  
                    for(int i : taskScheduleModel.getDayOfMonths()) {  
                        days += i + "号";  
                    }  
                    description.append("每月的").append(days).append(" ");    
                }      
                . description.append (taskScheduleModel.getHour ()) append ( " when");     
                description.append(taskScheduleModel.getMinute()).append("分");    
                description.append (taskScheduleModel.getSecond ()) the append ( "second");.    
                description.append ( "perform");     
            }     
                
        }     
        return Description. toString ();     
    }   
      
    // example with reference to   
    public static void main (String [] args) {   
        // execution time: day 12:12:12 Start   
        TaskScheduleModel taskScheduleModel new new TaskScheduleModel = ();   
        taskScheduleModel.setJobType (. 1); / / per day   
        Integer hour = 12; when //   
        Integer = 12 is minute; // points   
        Integer second = 12; // sec   
        taskScheduleModel.setHour (hour);   
        taskScheduleModel.setMinute (minute);  
        taskScheduleModel.setSecond (SECOND);   
        String cropExp = createCronExpression (taskScheduleModel);   
        System.out.println (cropExp + ":" + createDescription (taskScheduleModel));   
        // time of execution: day 12:12:12 End   
          
        taskScheduleModel.setJobType (3); // what day of the week   
        Integer [] = dayOfWeeks new new Integer [3];   
        dayOfWeeks [0] =. 1;   
        dayOfWeeks [. 1] = 2;   
        dayOfWeeks [2] = 3;   
        taskScheduleModel.setDayOfWeeks (dayOfWeeks );   
        cropExp = createCronExpression (taskScheduleModel);   
        System.out.println (cropExp + ":" + createDescription (taskScheduleModel));   
          
        taskScheduleModel.setJobType (2); // monthly days of execution  
        Integer[] dayOfMonths = new Integer[3];  
        dayOfMonths[0] = 1;  
        dayOfMonths[1] = 21;  
        dayOfMonths[2] = 13;  
        taskScheduleModel.setDayOfMonths(dayOfMonths);  
        cropExp = createCronExpression(taskScheduleModel);  
        System.out.println(cropExp + ":" + createDescription(taskScheduleModel));  
          
    }  
}  

  

Guess you like

Origin www.cnblogs.com/chcha1/p/11274943.html