今日学んだことのさらに別の例。
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SchedulerExample2 implements Runnable{
public static void main(String args[]) {
Calendar due = Calendar.getInstance();
due.set(Calendar.MILLISECOND, 0);
due.set(Calendar.SECOND, 0);
due.set(Calendar.MINUTE, 20);
if (due.before(Calendar.getInstance())) {
due.add(Calendar.HOUR, 1);
}
long milliSecondsToNextOcurrence = due.getTimeInMillis() - new Date().getTime();
final ScheduledExecutorService s = Executors.newSingleThreadScheduledExecutor();
ShedulerExample task = new ShedulerExample();
s.scheduleAtFixedRate(task, milliSecondsToNextOcurrence, 60*60*1000, TimeUnit.MILLISECONDS);
}
@Override
public void run() {
System.out.println("hola->"+new Date());
}
}