Schedule a task every day using ScheduledExecutorService?

I use ScheduledExecutorServiceto run a specific task in the 3 AMmorning every day. Now I'm not sure if my bottom code will be called MyTask()every 3 AMmorning? Since I'm not sure if my logic is correct or not

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Date aDate = ......// Current date or parsed date;
Calendar with = Calendar.getInstance();
with.setTime(aDate);
int hour = with.get(Calendar.HOUR);
int intDelayInHour = hour < 3 ? 3 - hour : 24 - (hour - 3);

scheduler.scheduleAtFixedRate(new MyTask(), intDilayInHour, 24, TimeUnit.HOURS); 

And to check this, I need to wait one day to understand if it works, and I do not want to do this.

Can someone help me determine if my code above is correct or not?

+4
source share
1 answer

, , +59 + -59 , .. 3.00 4.00 , ,

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Date aDate = new Date();// Current date or parsed date;

Calendar with = Calendar.getInstance();
with.setTime(aDate);
int hour = with.get(Calendar.HOUR_OF_DAY);
int Minutes = with.get(Calendar.MINUTE);

int MinutesPassed12AM = hour * 60 + Minutes;
int MinutesAt3AM = 3 * 60;
int OneDayMinutes = 24 * 60;
long DelayInMinutes = MinutesPassed12AM <= MinutesAt3AM ? MinutesAt3AM - MinutesPassed12AM : OneDayMinutes - (MinutesPassed12AM - MinutesAt3AM);


scheduler.scheduleAtFixedRate(new MyTask(), DelayInMinutes, OneDayMinutes, TimeUnit.MINUTES); 

[ ]) http://en.wikipedia.org/wiki/Daylight_saving_time), , , + -1 , , , , , , scheduler.shutdown();.

+3

All Articles