clock scheduled event

I am trying to write a program that executes some code at a certain time.  (eg. 3:00 am)  I am using the java swing class and can not figure out how to write this.  Can someone help me out?

-Jonathan
jmf8883Asked:
Who is Participating?
 
Ajay-SinghCommented:
> Can someone help me out?
try using timer: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Timer.html
0
 
jmf8883Author Commented:
I have looked at that and am pretty sure I need to use it but I don't know how.  I want the event to occur at a specific time reguardless of when I start the timer.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Ajay-SinghCommented:
Try this:

        long time = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.HOUR_OF_DAY, 3);
        if (cal.getTimeInMillis() < time) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }
        Timer timer = new Timer(24 * 60 * 60 * 1000, ...);
        timer.setInitialDelay(cal.getTimeInMillis()-time);
0
 
sciuriwareCommented:
That only means you must schedule over the number of milliseconds
between now and then:
Use Calendar to compute the time in mSec of any moment.

;JOOP!
0
 
sciuriwareCommented:
That's what I meant ...
0
 
ksivananthCommented:
also make a note of this

"Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes), the action event handlers for Timers execute on another thread -- the event-dispatching thread. This means that the action handlers for Timers can safely perform operations on Swing components. However, it also means that the handlers must execute quickly to keep the GUI responsive."

If you expect the task can be lengthy one, consider executing the task in another thread and make the GUI update calls through SwingUtilities.invokeLater(...) method.
0
 
mukundha_expertCommented:
Have a thread which checks for the current time to be 3.0 AM ( certain time ) , then call the program you want to execute from that thread,

0
 
mukundha_expertCommented:
In the swing class start a thread which checks for the condition,


     Calendar cal = Calendar.getInstance () ;
     if ( cal.get ( Calendar.get ( Calendar.HOUR ) == 3 )
             // call method

0
All Courses

From novice to tech pro — start learning today.