views:

305

answers:

5

I just want that my program or method should run at specific date and time. i have heard about Timer and TimerTask in java API. But don't know exactly how to use it.

A: 

This link from O'Reilly might help.

duffymo
A: 

Maybe you should use a third-party library with a higher level API like Quartz and use the SimpleTrigger.

Damien B
A: 

If this is for your own benefit and not for a project I would suggest you look into http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html which is a replacement for java.util.Timer. If however you want a robust scheduler, I concur with the previous posters with regards to Quartz.

Yiannis
+2  A: 

If you want to run a java program at a specific time you probably want to look at the OS tools (like cron or at).

If you want to run a method inside of an already running java application then the ScheduleExecutorService, while it may be overkill, is pretty easy to use.

TofuBeer
A: 

You can run your task inside a Glassfish Java EE server. It supports a Timer Service that fires background tasks at specified intervals. When you're running a cluster of Glassfish servers on different machines, they'll collaborate to fire the task exactly once.

A simpler approach is to rely on cron for Unix systems. At specified times you can run your java task via the java command.

I've also used pycron on Windows, which is a service that emulates cron.

Jim Ferrans