Issue
So if I wanted one of my activities to do something at a specific time and day how do I do so? any quick description would be helpful. Im still very new at this and any help is very appreciated.
Solution
If you want your activities to run between some interwell you can use Threads in java. If activities want to run at some sheduled time you can use TimerTask class in java.
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class ReportGenerator extends TimerTask {
public void run() {
System.out.println("Generating report");
//TODO generate report
}
}
class MainApplication {
public static void main(String[] args) {
Timer timer new Timer();
Calendar date = Calendar.getInstance();
date.set(
Calendar.DAY_OF_WEEK,
Calendar.SUNDAY
);
date.set(Calendar.HOUR, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// Schedule to run every Sunday in midnight
timer.schedule(
new ReportGenerator(),
date.getTime(),
1000 * 60 * 60 * 24 * 7
);
// Schedule to run every Monday in midnight
date.set(
Calendar.DAY_OF_WEEK,
Calendar.MONDAY
);
date.set(Calendar.HOUR, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
timer.schedule(
new ReportGenerator(),
date.getTime(),
1000 * 60 * 60 * 24 * 7
);
}
}
Answered By - Nisham Mahsin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.