tags:

views:

31

answers:

2

In my application,I want load some data to the database at the midnight each day,how to set it using the Timer?

I have thought using:

Timer t=new Timer();
TimerTask tt=new TimerTask(){
  public void run(
    //read the data, and push to db
  );
};
t.schedule(tt,(the next midnight-now time));

However, I am afraid this can only be executed once. Any fix?

+2  A: 

You might want to use public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

Schedules the specified task for repeated fixed-rate execution, beginning at the specified time. Subsequent executions take place at approximately regular intervals, separated by the specified period.

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html

Alberto Zaccagni
My application maybe not startup at midnight.
hguser
I did not understand that, what do you mean? With the method i posted you should be able to schedule the job for any time you want.
Alberto Zaccagni
A: 

You may want to check QuartzScheduler out. It is very useful in complex timing processes. It is also very easy to use.

Since your task is a very simple one, you may not want to use an extra library, but keep that in your mind.

http://www.quartz-scheduler.org/

hamam