我有以下代码片段,可确定作业是否在其运行时内:
private boolean isDuringRuntime(Job job) {
Date now = new Date();
System.out.println(DateUtil.getToday());
System.out.println(job.getStartTime().toString());
System.out.println(job.getEndTime().toString());
Date startTime = new Date(job.getStartTime().getTime()
+ DateUtil.getToday().getTime());
Date endTime = new Date(job.getEndTime().getTime()
+ DateUtil.getToday().getTime());
System.out.println("Start Time: " + startTime.toString());
System.out.println("Stop Time: " + endTime.toString());
return now.after(startTime) && now.before(endTime);
}
DateUtil.getToday()
public static Date getToday() {
Date now = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
这是一次运行的输出:
Sun Jul 14 00:00:00 EDT 2013 //This is DateUtil.getToday();
1970-01-01 13:15:00.0 //Raw Start Time
1970-01-01 18:15:00.0 //Raw End Time
Start Time: Sun Jul 14 18:15:00 EDT 2013 //Computed Start Time
Stop Time: Sun Jul 14 23:15:00 EDT 2013 //Computed End Time
我不确定为什么计算的开始时间和计算的结束时间提前五个小时。我希望开始时间是Sun Jul 14 13:15:00 EDT 2013
,结束时间是Sun Jul 14 18:15:00 EDT 2013
。
谁能指出我哪里出错了?我不是很熟悉,Date
我怀疑我忽略了一些简单的东西。