I want to run my program everyday at midnight when time changes from am to pm in between two dates, for example(from today date i.e 22/01/2015 to 25/01/2015).
-
You should do this out of Java. In Linux, use cronjob, In Windows, use Scheduler – Raptor Jan 22 '15 at 06:01
-
possible duplicate of [cron that will run a ruby script every day at midnight](http://stackoverflow.com/questions/3984134/cron-that-will-run-a-ruby-script-every-day-at-midnight) – Siddharth Jan 22 '15 at 06:01
-
definite duplicate of http://stackoverflow.com/questions/3984134/cron-that-will-run-a-ruby-script-every-day-at-midnight – Siddharth Jan 22 '15 at 06:02
-
i dont want to do it using ruby.. i want to do it in java only... – Rahul Jan 22 '15 at 06:15
3 Answers
There are many ways in doing this
- ScheduledExecutorService, in built Java class you can use this it will schedule the job at specified time
- Quartz FW is a 3rd party API to be use for large scale scheduling
- Cron jobs, in linux and windows batch file you can do this
- Autosys, job scheduler

- 12,647
- 2
- 39
- 57
-
It's better to use a combination of CRON and java rather just using ScheduledExecutorService because a java background thread will be running definitely even when your not executing your job. So write ur code, build a jar and schedule it using CRON if ur using Linux – Arkantos Jan 22 '15 at 18:42
As suggested by others, if you can, use ScheduledExecutorService.
If not, start a thread (Runnable or Thread) that runs your method m (which checks the date and time, and does the needful if time has changed from am to pm). Sleep for a few minutes, wake up, run m, ... let the process loop forever (or for as long as you want). Even better if you can sleep for the exact amount of time required so that when the thread wakes up, you know there has been a transition from am to pm. For instance, your program started up at 10:00 am, then you could compute that it would be pm in 2 hours (120 minutes) from now. So, just sleep for 120 minutes. Wake up and do the am-to-pm activity and sleep for 24 hours.

- 161
- 1
- 2
- 7
I would very much recommend a CRON job for this as it is reliable, and comes built in with most Linux environments if that's where you're running it from.