public class Reminder{
Timer timer;
protected int start=0;
protected int end=0;
public Reminder(int s,int e,int frequency){
start = s;
end = e;
timer = new Timer();
timer.schedule(new ReminderTask(),0,frequency*1000);
}
public class ReminderTask extends TimerTask{
public void run(){
System.out.println("doing something");
Calendar cal = Calendar.getInstance();
if(cal.get(Calendar.HOUR_OF_DAY) > end){
int hour = cal.get(Calendar.HOUR_OF_DAY);
//计算需要睡眠多久
while(true){
try {
Thread.sleep((start+24-hour)*1000);
break;
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}
}
public static void main(String args[]){
new Reminder(8,16,1);
}
}