Sami
Sami

Reputation: 7837

How to call a method on specific time in java?

Is it possible to call a method in java at specific time? For example of I have a piece of code like this:

class Test{

    public static void main(String args[]) {
        // here i want to call foo at : 2012-07-06 13:05:45 for instance
        foo();
    }
}

How this can be done in java?

Upvotes: 36

Views: 86267

Answers (9)

Rahul Davane
Rahul Davane

Reputation: 71

Timer timer = new Timer();
Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
calendar.add(Calendar.SECOND, 5);
Date date = calendar.getTime();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        test();
    }
}, date);

Upvotes: 1

Kalai Selvan Ravi
Kalai Selvan Ravi

Reputation: 2886

It is possible. You can schedule a method at some time using Timer and TimerTask.

For example:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);

Date alarmTime = calendar.getTime();

Timer _timer = new Timer();
_timer.schedule(foo, alarmTime);

Refer these links:

Upvotes: 3

assylias
assylias

Reputation: 328598

You can use a ScheduledExecutorService, which is "a more versatile replacement for the Timer/TimerTask combination" (according to Timer's javadoc):

long delay = ChronoUnit.MILLIS.between(LocalTime.now(), LocalTime.of(13, 5, 45));
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.schedule(task, delay, TimeUnit.MILLISECONDS);

Upvotes: 20

dimas
dimas

Reputation: 6073

As I know you can use Quartz-scheduler for this. I have not used it yet, but many people have recommended it to me.

Upvotes: 4

Ramesh PVK
Ramesh PVK

Reputation: 15446

Using a java.util.Timer class you can create a timer and schedule it to run at specific time.

Below is the example:

//The task which you want to execute
private static class MyTimeTask extends TimerTask
{

    public void run()
    {
        //write your code here
    }
}

public static void main(String[] args) {

    //the Date and time at which you want to execute
    DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = dateFormatter .parse("2012-07-06 13:05:45");

    //Now create the time and schedule it
    Timer timer = new Timer();

    //Use this if you want to execute it once
    timer.schedule(new MyTimeTask(), date);

    //Use this if you want to execute it repeatedly
    //int period = 10000;//10secs
    //timer.schedule(new MyTimeTask(), date, period );
}

Upvotes: 60

Pshemo
Pshemo

Reputation: 124215

Simple demo of java.util.Timer

Timer t=new Timer();
t.schedule(new TimerTask() {
    public void run() {
        foo();
    }
}, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-07-06 13:40:20"));

Upvotes: 2

Candlejack
Candlejack

Reputation: 266

If you are talking about SE then the Timer class might be what you are looking for http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html, available since Java 5.

If you need to time something in an application server context I would recommend having a look at EJB timers http://www.javabeat.net/2007/03/ejb-3-0-timer-services-an-overview/ available since EJB 3.0.

Alternatively, depending on what you are really trying to do, you could elaborate if using a cron job (or any other OS based timer method) would be more suitable, i.e. you don't want or can't have the VM running all the time.

Upvotes: 2

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

You could use the class Timer

From documentation:

A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

The method schedule(TimerTask task, Date time) is exactly what you want: Schedules the specified task for execution at the specified time.

If you need schedule in cron format, quartz would be a good solution. (quartz cron like schedule)

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533492

Its is possible, and I would use a library such as http://quartz-scheduler.org/

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do.

Upvotes: 7

Related Questions