I'm trying to develop a small system that sends an email every Tuesday at 08:00 in the morning.
But I'm a bit confused, I can already send the email and check if it's Tuesday, but I do not know if it's correct.
The system keeps running, but since I'm still not checking to see if it's 08:00 in the morning, it should not send multiple emails without stopping, since the only condition for sending emails is to be Tuesday?
Here is the code:
public void gerarAviso() {
Timer timer = null;
if (timer == null) {
timer = new Timer();
Calendar data = Calendar.getInstance();
TimerTask tarefa = new TimerTask() {
@Override
public void run() {
System.err.println("DATA: " + data);
if (data.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY){
System.out.println("É terça feira");
EnviaEmail e = new EnviaEmail();
e.enviaEmail("[email protected]");
}else {
System.out.println("Não é terça feira!");
}
}
};
timer.schedule(tarefa, data.getTime());
}
}