I am working on making a cron job in Java. I want to run a particular task every week, month, three month, six month and nine month.
public Interface interfaceA {
public String abc() throws Exception;
}
public class TestTaskA implements interfaceA {
@Override
public String abc() throws Exception {
// some code
}
}
I am running it like this -
TestTaskA testTaskA = new TestTaskA();
testTaskA.abc();
I want to run TestTaskA
every week, every month, every three month, every six month, every nine month and I don't want to run a task between 8 PM till 5 AM. Any random day is also fine.
Now if I am running TestTaskA
every week, then it should print out one-week
and report_week
and if it is running every month, then it should print out one-month
and report_one_month
. Similarly for three month, six month and nine month.
What is the best way to do this? Keeping in mind, I might have TestTaskB and TestTaskC as well which I am supposed to run every week, month, three month, six month and nine month as well.
Can I use ScheduledExecutorService
for this? Any simple example will be of great help to me.