java后台开发代码 java编写后端( 五 )


*/
protected Date planDate;
public boolean haveNext() {
return (planDate != null);
}
public Date getCurrentDate() {
return currentDate;
}
public void setCurrentDate(Date date) {
currentDate = date;
planDate = date; //在赋给currentDate值时,同时也赋给planDate
}
}
c) 然后我们看看三种计划方案的实现类的源代码:
//“一次性运行”的计划方案类
import java.util.Date;
public class TimePlanOnce extends AbstractTimePlan {
public Date nextDate() {
//把要当前的计划时间保存在中间变量中
Date returnDate = this.planDate;
//算出下一个计划时间 。没有下一个就设为null
this.planDate = null;
//判断一下计划时间合不合条件
if (returnDate == null)
throw new NullPointerException("没有下一个计划日期");
return returnDate;
}
public String getTimePlanString() {
return "一次性运行,运行时间: (打印this.currentDate) ";
}
}
//“周期性间隔”的时间计划方案类
import java.util.Date;
public class TimePlanPeriod extends AbstractTimePlan {
public static final int HOUR = 0;
public static final int DAY = 1;
private int spaceTime; //间隔时间,单位毫秒
private int timeType;
public Date nextDate() {
//把要当前的计划时间保存在中间变量中
Date returnDate = this.planDate;
//算出下一个计划时间 。没有下一个就设为null
int milliSecond = 0;
if (timeType ==HOUR) milliSecond = spaceTime * 1000; //小时*60*60*1000;
if (timeType ==DAY) milliSecond = spaceTime * 24 * 60 * 60 * 1000; //天
planDate = Util.DateAddSpaceMilliSecond(planDate, milliSecond);
//判断一下计划时间合不合条件
if (returnDate == null)
throw new NullPointerException("没有下一个计划日期");
return returnDate;
}
public String getTimePlanString() {
if (timeType == HOUR)
return "第一次运行于:currentDate.并每隔spaceTime小时运行一次";
if (timeType == DAY)
return "第一次运行于:currentDate.并每隔spaceTime天运行一次";
return "";
}
public int getSpaceTime() {return spaceTime; }
public int getTimeType() { return timeType;}
public void setSpaceTime(int i) { spaceTime = i; }
public void setTimeType(int i) {timeType = i; }
}
/**选择一周的某几天,让这几天在同一时间点运行任务, 一周内必须选择一天*/
import java.util.Calendar;
import java.util.Date;
public class TimePlanSelectWeek extends AbstractTimePlan {
private static Calendar c = Calendar.getInstance(); //取得一个日历实例
private static int spaceMilliSecond = 0; //间隔时间,单位毫秒
private boolean[] selectWeek = new boolean[7]; //0为星期日 ,1为星期一
public Date nextDate() {
Date returnDate = null;
if (!isSelectWeek(planDate)) //如果这一天不是所选周中的一天
planDate = getNextDate(planDate);
returnDate = planDate;
planDate = getNextDate(planDate);
//判断一下计划时间合不合条件
if (returnDate == null)
throw new NullPointerException("没有下一个计划日期");
return returnDate;
}
//算出下一个计划时间 。没有下一个就设为null
private Date getNextDate(Date date) {
Date tempDate = date;
Date returnDate = null;
for (int i = 0; i7; i++) {
tempDate = Util.DateAddSpaceMilliSecond(tempDate, spaceMilliSecond);
if (isSelectWeek(tempDate)) {
returnDate = tempDate;
break;
}
}
return returnDate;
}
/**设置某星期是否被选, 0为星期日 ,1为星期一....6为星期六*/
public void setSelectWeek(int i, boolean b) {selectWeek[i] = b;}

推荐阅读