我有一个计划任务,该任务从JSP页面获取主机名和其他Calendar信息。对于“ HOUR_of_Day”输入,我总是添加12并将总数传递给Calender,因为它使用24小时格式。我昨天晚上做的很好,但是今天早上不工作。它不会在计划的时间运行。我当然做错了,需要一些指导。下面是我的代码:
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class ScheduledTask extends TimerTask {
String hostname = null;
//The default no - arg constructor
//=============================
public ScheduledTask(){
}
/**
* Another overloaded constructor
* that accepts an object
* @param scanHostObj
public ScheduledTask(ScanHost scanHostObj){
hostname = scanHostObj.getHostname();
}
/**
* Another overloaded constructor
* that accepts a string parameter
* @param nodename
*/
public ScheduledTask(String nodename){
hostname = nodename;
}
/**
* The run method that executes
* the scheduled task
*/
public void run() {
new ScanUtility().performHostScan(hostname);
}
public void executeScheduledScanJob(ScanHost hostObj, Scheduler schedulerObj){
/**
* Get the various schedule
* data, convert to appropriate data type
* and feed to calender class
*/
String nodename = hostObj.getHostname();
String dayOfScan = schedulerObj.getDayOfScan();
String hourOfScan = schedulerObj.getHourOfScan();
String minuteOfScan = schedulerObj.getMinuteofScan();
//Convert String values to integers
//===================================
final int THE_DAY = Integer.parseInt(dayOfScan);
final int THE_HOUR = Integer.parseInt(hourOfScan);
final int REAL_HOUR = THE_HOUR + 12;
final int THE_MINUTE = Integer.parseInt(minuteOfScan);
/**
* Feed these time values to the Calendar class.
* Since Calendar takes a 24 - hour format for hours
* it is better to add 12 to any integer value for
* hours to bring the real hourly format to the 24
* format required by the Calendar class
*/
Calendar scheduleCalendar = Calendar.getInstance();
scheduleCalendar.set(Calendar.DAY_OF_WEEK, THE_DAY);
scheduleCalendar.set(Calendar.HOUR_OF_DAY, REAL_HOUR);
scheduleCalendar.set(Calendar.MINUTE, THE_MINUTE);
/**
* Then Initialize the timer Object
* and pass in parameters to it method
* cancel out all tasks/jobs after running them
*/
Timer scheduledTimeObj = new Timer();
ScheduledTask scheduledTaskObj = new ScheduledTask(nodename);
scheduledTimeObj.schedule(scheduledTaskObj, scheduleCalendar.getTime());
scheduledTimeObj.cancel();
scheduledTaskObj.cancel();
}
}
答案 0 :(得分:0)
您可以尝试替换
scheduleCalendar.set(Calendar.HOUR_OF_DAY, REAL_HOUR);
与此:
scheduleCalendar.set(Calendar.HOUR, THE_HOUR);
这将需要12个小时的时间。
此外,您可以将AM_PM设置为AM或PM。如果您尝试解析PM日期,请在此处使用PM。
scheduleCalendar.set(Calendar.AM_PM, Calendar.PM);