Results 1 to 7 of 7

Thread: 24-Hour Clock Timer Task in Java

  1. #1
    Join Date
    Apr 2010
    Posts
    87

    24-Hour Clock Timer Task in Java

    I would like to call a function just once on the day of change that will end my program after 5 minutes I have tried with timer (). schedule (mainTask, time) the function of a particular time to execute. Tracks I have problems when my time is GMT so java.util.Date format and I actually have an hour it would be more problems because I get the timer to 23, the program and if it is then started again then jump back on the timer starts and immediate the prog down again it would happen at 24!!! Please provide some suggestions.

  2. #2
    Join Date
    Mar 2008
    Posts
    227

    Re: 24-Hour Clock Timer Task in Java

    I think that you are not using the calendar properly. I suggest that you should try the GregorianCalendar class which will solve your doubt. GregorianCalendar class using the Locale set somehow the current time zone? If you use that accordingly, then it should work properly.

  3. #3
    Join Date
    Mar 2008
    Posts
    192

    Re: 24-Hour Clock Timer Task in Java

    If I understand you correctly, you should use the GregorianCalendar # add. For example :
    Code:
    GregorianCalendar cal = new GregorianCalendar ();
     cal.add (Calendar.HOUR, 24); // or:
     cal.add (Calendar.DAY_OF_YEAR, 1);

  4. #4
    Join Date
    Apr 2010
    Posts
    73

    Re: 24-Hour Clock Timer Task in Java

    I am looking desperately a good tutorial and example code for the javax.management.timer API. I want to program a clock that counts down every second to 1 (this goes without Timer interface, but preferably without wait (...)?) Hoping that you got my point and will help me soon.!! Since I am not having much idea about it, I am expecting that someone will explain me in details.

  5. #5
    Join Date
    Apr 2008
    Posts
    193

    Re: 24-Hour Clock Timer Task in Java

    Why do you take for her to the management API? A timer will but already quite normal in the java.util package. You simply create a timer (class java.util.Timer). Then another TimerTask (where you must implement the run () method) and then you start the timer with the schedule () method. Then we run the run () method as many times and at such intervals as you have stated it.

  6. #6
    Join Date
    Nov 2008
    Posts
    1,192

    Re: 24-Hour Clock Timer Task in Java

    For the example of our clock, you create the Timer instance with a constructor without parameters. Then we call the method scheduleAtFixedRate (). This method must pass three parameters:
    • A TimerTask class. This class has a method run () (Since TimerTask is an abstract class, we must make it a child class and implement the run () method) and that is to be called from time to time. Here we put our code.
    • We will use the version of the method in which the second parameter is a long, which is based on how many milliseconds we want to start telling us. If we scratch, begin to let us know immediately.
    • The third parameter takes another long, which is how many milliseconds each want to receive the notice.

  7. #7
    Join Date
    Nov 2008
    Posts
    996

    Re: 24-Hour Clock Timer Task in Java

    The sample code for the clock might look like this :
    Code:
     / / Class which is the code to execute 
    TimerTask timerTask = new TimerTask () 
    ( 
    public void run () 
    ( 
    // Here we want to execute code. 
    ) 
    ); 
    .... 
    
    // Here starts the timer every second. 
    Timer timer = new Timer (); 
    // In 0 milliseconds 1000 milliseconds let me know 
    timer.scheduleAtFixedRate (timerTask, 0, 1000);

Similar Threads

  1. Windows phone 7 does not have stock timer/clock app
    By CheeCha in forum Portable Devices
    Replies: 4
    Last Post: 08-02-2012, 11:56 AM
  2. Replies: 4
    Last Post: 27-07-2011, 10:56 PM
  3. Replies: 3
    Last Post: 01-12-2010, 06:10 AM
  4. Help for Timer examples in Java
    By Feng in forum Software Development
    Replies: 5
    Last Post: 24-07-2010, 03:36 AM
  5. PC Clock keeps going back one hour exactly every boot on Win 7
    By ptolomy_was_right in forum Operating Systems
    Replies: 3
    Last Post: 15-05-2010, 12:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,862,683.65186 seconds with 17 queries