clock scheduled event

Posted on 2006-07-14
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?

Question by:jmf8883
  • 2
  • 2
  • 2
  • +3
LVL 23

Expert Comment

ID: 17111290
> Can someone help me out?
try using timer:

Author Comment

ID: 17111332
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.
LVL 23

Expert Comment

ID: 17111405
Try this:

        long time = System.currentTimeMillis();
        Calendar cal = Calendar.getInstance();
        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, ...);
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 24

Expert Comment

ID: 17111419
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.

LVL 24

Expert Comment

ID: 17111423
That's what I meant ...
LVL 26

Expert Comment

ID: 17112136
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.
LVL 10

Expert Comment

ID: 17112177
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,

LVL 10

Expert Comment

ID: 17112187
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

LVL 92

Accepted Solution

objects earned 500 total points
ID: 17112282

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JAVA API design with micro service cloud in mind 1 78
java example issue 5 43
Setup GlassFish 4 35
What is the use of Forwarding Class in java 1 35
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

749 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