Solved

clock scheduled event

Posted on 2006-07-14
9
225 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:jmf8883
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
> Can someone help me out?
try using timer: http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/Timer.html
0
 

Author Comment

by:jmf8883
Comment Utility
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
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
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
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
That's what I meant ...
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
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
 
LVL 10

Expert Comment

by:mukundha_expert
Comment Utility
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
 
LVL 10

Expert Comment

by:mukundha_expert
Comment Utility
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now