Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Make sure that Timer run everyday at a Specific Time

 
James Gordon
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to schedule a task at a particular time every day with java.util.Timer.
If I would to use the schedule(TimerTask, Date, DAY_IN_MILLIS) method, I would need to be very careful as it should be sometime in the future, else the schedule won't run at all ie: if it should run at 10:00pm everyday but the Timer start (Timer object created and schedule method called) at 11:00pm, nothing would ever happened!
Therefore, I would like to know whether there is any good solutions to this. What I can think of is to add 1 day to the Date if it is already past. The next thing I can think of is to use the schedule(TimerTask, 0, DAYS_IN_MILLIS) method and check the time manually in the run() method.
What's your opinion?

Thanks.
 
Peter Kristensson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try the first method. I've used this previously and it works.
/Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic