书接上回,
上回说到,武松武二郎斗杀西门庆,为大哥报了仇…… !!!-_- 啊,说串了,嘿嘿 不好意思
(删掉上面一行)
上回说到,我们的设置页面已经做好了,接下来就是将时间转换为Unix Cron Expression。
2) 将时间转换为Unix Cron Expression
需要ActionForm将页面表单数据映射到Action中,然后在Action中转换为cron expression:
1 SchedulerForm schedulerForm = (SchedulerForm) form;
2 String jobName = schedulerForm.getJobName();
3 String cronExpression = "" ;
4 String[] commonNeeds = {schedulerForm.getSecond(), schedulerForm.getMinute(), schedulerForm.getHour()} ;
5 String[] monthlyNeeds = {schedulerForm.getWeek(), schedulerForm.getDayOfMonth()} ;
6 String weeklyNeeds = schedulerForm.getDayOfWeek();
7 String userDefinedNeeds = schedulerForm.getDate();
8 String everyWhat = schedulerForm.getEveryWhat();
9 // 得到时间规则
10 cronExpression = CronExpConversion.getCronExpression(everyWhat, commonNeeds,
11 monthlyNeeds, weeklyNeeds, userDefinedNeeds);
12
我定义了一个 规则类来处理转换规则(写得不是很好 能用就行 嘿嘿)
1
2 /** */ /**
3 * 页面设置转为UNIX cron expressions 转换类
4 * CronExpConversion
5 */
6 public class CronExpConversion {
7
8 /** */ /**
9 * 页面设置转为UNIX cron expressions 转换算法
10 * @param everyWhat
11 * @param commonNeeds 包括 second minute hour
12 * @param monthlyNeeds 包括 第几个星期 星期几
13 * @param weeklyNeeds 包括 星期几
14 * @param userDefinedNeeds 包括具体时间点
15 * @return cron expression
16 */
17 public static String convertDateToCronExp(String everyWhat,
18 String[] commonNeeds, String[] monthlyNeeds, String weeklyNeeds,
19 String userDefinedNeeds) {
20 String cronEx = "" ;
21 String commons = commonNeeds[ 0 ] + " " + commonNeeds[ 1 ] + " "
22 + commonNeeds[ 2 ] + " " ;
23 String dayOfWeek = "" ;
24 if ( " monthly " .equals(everyWhat)) {
25 // eg.: 6#3 (day 6 = Friday and "#3" = the 3rd one in the
26 // month)
27 dayOfWeek = monthlyNeeds[ 1 ]
28 + CronExRelated.specialCharacters
29 .get(CronExRelated._THENTH) + monthlyNeeds[ 0 ];
30 cronEx = (commons
31 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
32 + " "
33 + CronExRelated.specialCharacters.get(CronExRelated._EVERY)
34 + " " + dayOfWeek + " " ).trim();
35 } else if ( " weekly " .equals(everyWhat)) {
36 dayOfWeek = weeklyNeeds; // 1
37 cronEx = (commons
38 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
39 + " "
40 + CronExRelated.specialCharacters.get(CronExRelated._EVERY)
41 + " " + dayOfWeek + " " ).trim();
42 } else if ( " userDefined " .equals(everyWhat)) {
43 String dayOfMonth = userDefinedNeeds.split( " - " )[ 2 ];
44 if (dayOfMonth.startsWith( " 0 " )) {
45 dayOfMonth = dayOfMonth.replaceFirst( " 0 " , "" );
46 }
47 String month = userDefinedNeeds.split( " - " )[ 1 ];
48 if (month.startsWith( " 0 " )) {
49 month = month.replaceFirst( " 0 " , "" );
50 }
51 String year = userDefinedNeeds.split( " - " )[ 0 ];
52 // FIXME 暂时不加年份 Quartz报错
53 /**/ /* cronEx = (commons + dayOfMonth + " " + month + " "
54 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
55 + " " + year).trim(); */
56 cronEx = (commons + dayOfMonth + " " + month + " "
57 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
58 + " " ).trim();
59 }
60 return cronEx;
61 }
62 }
63 这样就将页面的时间设置转为了Cron Expression。
上回说到,武松武二郎斗杀西门庆,为大哥报了仇…… !!!-_- 啊,说串了,嘿嘿 不好意思
(删掉上面一行)
上回说到,我们的设置页面已经做好了,接下来就是将时间转换为Unix Cron Expression。
2) 将时间转换为Unix Cron Expression
需要ActionForm将页面表单数据映射到Action中,然后在Action中转换为cron expression:
1 SchedulerForm schedulerForm = (SchedulerForm) form;
2 String jobName = schedulerForm.getJobName();
3 String cronExpression = "" ;
4 String[] commonNeeds = {schedulerForm.getSecond(), schedulerForm.getMinute(), schedulerForm.getHour()} ;
5 String[] monthlyNeeds = {schedulerForm.getWeek(), schedulerForm.getDayOfMonth()} ;
6 String weeklyNeeds = schedulerForm.getDayOfWeek();
7 String userDefinedNeeds = schedulerForm.getDate();
8 String everyWhat = schedulerForm.getEveryWhat();
9 // 得到时间规则
10 cronExpression = CronExpConversion.getCronExpression(everyWhat, commonNeeds,
11 monthlyNeeds, weeklyNeeds, userDefinedNeeds);
12
我定义了一个 规则类来处理转换规则(写得不是很好 能用就行 嘿嘿)
1
2 /** */ /**
3 * 页面设置转为UNIX cron expressions 转换类
4 * CronExpConversion
5 */
6 public class CronExpConversion {
7
8 /** */ /**
9 * 页面设置转为UNIX cron expressions 转换算法
10 * @param everyWhat
11 * @param commonNeeds 包括 second minute hour
12 * @param monthlyNeeds 包括 第几个星期 星期几
13 * @param weeklyNeeds 包括 星期几
14 * @param userDefinedNeeds 包括具体时间点
15 * @return cron expression
16 */
17 public static String convertDateToCronExp(String everyWhat,
18 String[] commonNeeds, String[] monthlyNeeds, String weeklyNeeds,
19 String userDefinedNeeds) {
20 String cronEx = "" ;
21 String commons = commonNeeds[ 0 ] + " " + commonNeeds[ 1 ] + " "
22 + commonNeeds[ 2 ] + " " ;
23 String dayOfWeek = "" ;
24 if ( " monthly " .equals(everyWhat)) {
25 // eg.: 6#3 (day 6 = Friday and "#3" = the 3rd one in the
26 // month)
27 dayOfWeek = monthlyNeeds[ 1 ]
28 + CronExRelated.specialCharacters
29 .get(CronExRelated._THENTH) + monthlyNeeds[ 0 ];
30 cronEx = (commons
31 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
32 + " "
33 + CronExRelated.specialCharacters.get(CronExRelated._EVERY)
34 + " " + dayOfWeek + " " ).trim();
35 } else if ( " weekly " .equals(everyWhat)) {
36 dayOfWeek = weeklyNeeds; // 1
37 cronEx = (commons
38 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
39 + " "
40 + CronExRelated.specialCharacters.get(CronExRelated._EVERY)
41 + " " + dayOfWeek + " " ).trim();
42 } else if ( " userDefined " .equals(everyWhat)) {
43 String dayOfMonth = userDefinedNeeds.split( " - " )[ 2 ];
44 if (dayOfMonth.startsWith( " 0 " )) {
45 dayOfMonth = dayOfMonth.replaceFirst( " 0 " , "" );
46 }
47 String month = userDefinedNeeds.split( " - " )[ 1 ];
48 if (month.startsWith( " 0 " )) {
49 month = month.replaceFirst( " 0 " , "" );
50 }
51 String year = userDefinedNeeds.split( " - " )[ 0 ];
52 // FIXME 暂时不加年份 Quartz报错
53 /**/ /* cronEx = (commons + dayOfMonth + " " + month + " "
54 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
55 + " " + year).trim(); */
56 cronEx = (commons + dayOfMonth + " " + month + " "
57 + CronExRelated.specialCharacters.get(CronExRelated._ANY)
58 + " " ).trim();
59 }
60 return cronEx;
61 }
62 }
63 这样就将页面的时间设置转为了Cron Expression。