Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Timer Class (How to reschedule)

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Timer Class (How to reschedule)

    I have the following code that works in that I can enter the hour, min and sec and it will wait until this time to run the code.

    What I need is to be able to run this permanently and for it to run at 7am every morning (except saturday and sunday).

    How can I alter this code to achieve the following, I also need to have this in its own thread due to other objects running their own code within the same package

    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.Calendar;
    import java.util.TimeZone;
     
     
    public class MyTimer 
    {
        Timer timer;
        Calendar startTime;
     
        public MyTimer(int seconds) 
        {
            startTime = Calendar.getInstance(TimeZone.getDefault());
            startTime.set(Calendar.HOUR_OF_DAY, 20);
            startTime.set(Calendar.MINUTE, 56);
            startTime.set(Calendar.SECOND, 00);
            timer = new Timer();
            timer.schedule(new RunTask(), startTime.getTime(), 1000 * 60 * 60 * 24);
        }
     
        class RunTask extends TimerTask 
        {
            public void run() 
            {
                System.out.println("It's Time");
                timer.cancel(); //Terminate the timer thread
            }
        }
     
        public static void main(String args[]) 
        {
            new MyTimer(5);
            System.out.println("Waiting");
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Timer Class (How to reschedule)

    Firstly, look into using the Calendar class, which will allow you to track time and dates based upon your requirements. Next, I'd recommend looking into Threads, as these could help you adapt your code for your requirements. For example, you could run a thread (call it T1) which calculates the time until your task needs to fire, the create a timer that fires based upon that time. T1 can call wait, and when the timer fires it can notify T1 to start the circle over again

  3. The Following User Says Thank You to copeg For This Useful Post:

    mds1256 (June 10th, 2011)

  4. #3
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Timer Class (How to reschedule)

    Thanks, used the calendar class and works fine

Similar Threads

  1. jpanel and using the timer class
    By kswiss in forum AWT / Java Swing
    Replies: 2
    Last Post: March 31st, 2011, 12:35 PM
  2. Timer digit
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 2nd, 2011, 07:07 PM
  3. Timer class countdown
    By p0oint in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2010, 03:31 AM
  4. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 22nd, 2010, 11:18 PM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM