我想知道是否可以持续检查今天的时间和将来的日期。例如,如果将来的日期是1/8/2015,今天的日期是1/7/2015。有没有一种方法可以自动保持检查今天的日期何时为1/8/2015(与将来的日期匹配)。
我当时在考虑使用while(true)循环,但感觉不对,因为循环永远不会结束。我还能怎么做?
编辑:所以基于一些在线研究并在这里回答我做到了。
Calendar cal2 = Calendar.getInstance();
cal2.add(Calendar.DAY_OF_MONTH, 1);
Date date = cal2.getTime();
System.out.print(cal2.getTime().toString());
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
for (int i = 1; i < 1000; i++) {
if (shelf.book[i] != null && shelf.book[i].overdue == true) {
}
}
}
}, date);
注意:我尚未测试。(还没有完成run()方法)
对于一个时间安排,一种简单的解决方案是使用计时器。
import java.util.*;
import java.text.*;
public class ScheduleTask {
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static Timer timer = new Timer();
private static class MyTimeTask extends TimerTask {
public void run() {
System.out.println("HOHO date just changed");
}
}
public static void main(String[] args) throws ParseException {
System.out.println("Current Time: " + df.format( new Date()));
//Date and time at which you want to execute
Date date = df.parse("2015-01-08 00:00:00");
timer.schedule(new MyTimeTask(), date);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句