user433619
user433619

Reputation: 91

Java timer class, to fire at a fixed time everyday

I want to write a simple timer class that fires , and makes a method call at a fixed time of every day, while the application is running.

I dont want to use Quartz as I think its a overkill for this simple problem, what are the different approaches I can try for ?

Thanks in Advance

Upvotes: 2

Views: 988

Answers (2)

Chris Dail
Chris Dail

Reputation: 26029

The util.concurrent's ScheduledExecutorService allows you to pretty easily schedule tasks to run on a fixed delay. The signature of the schedule method is as follows:

scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) 

You could find the difference between the current time and the time in which you wish to have it first run. Set the difference as the value for initialDelay. Set the period to 1 and the TimeUnit unit to TimeUnit.DAYS. That will cause it to run every day at that time.

Upvotes: 1

duffymo
duffymo

Reputation: 308763

Why not use java.util.Timer and java.util.TimerTask?

Upvotes: 4

Related Questions