1

I try to create a timer in java and show it on JFrame but when I comper my timer to my phone timer the timer in my phone is faster then my why?

I set the deley to 10 to every hundred of second in my timer.

This is the code only for the timer:

    import javax.swing.Timer;

    int min = 0, sec = 0, hundredSec = 0;

    timer = new Timer(10, new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            hundredSec++;

            if(hundredSec >= 99)
            {
                sec++;
                hundredSec = 0;
            }

            if(sec >= 59)
            {
                min++;
                sec = 0;
            }

            timerL.setText(String.format("%02d:%02d:%02d", min, sec, millisec));

        }
    });

Sorry for bad english.

Thanks in advance for answer.

2
  • The java timer isn't designed to be that exact. You should instead save the time the timer was started at and then have the actionPerformed method compute the difference between the current time and the start time and update the text from that. Commented Apr 10, 2016 at 16:32
  • how much is the different, java is not designed to develope realTime scheduled Task, so the timeUnit can variate a little bit ...... Commented Apr 10, 2016 at 16:46

2 Answers 2

1

I believe that your problem has to do with the third line of code. As the java API docs says: "The delay parameter is used to set both the initial delay and the delay between event firing, in milliseconds." This means that there is a 10 millisecond delay every time, which might be causing your delay. To fix that you can change the line of code to:

timer = new Timer(0, new ActionListener());

By changing 10 to 0 it would run instantly as opposed to slowly falling behind. I would recommend reading this article to learn more about timers.

Sign up to request clarification or add additional context in comments.

1 Comment

But if I put 0 in the delay so the timer run to fast. I set the Initial Delay and check it if I set the delay to 4 its to fast and I set it to 5 its to slow what can I do?
0

The delay you pass into a Timer is just that, a delay before the event is queued, not an exact time that the event will perform. While you can be sure that 10ms have passed since the last time the call was performed, you can't be sure that ONLY 10ms have passed.

You probably want something like this (with as little change to your code as possible; there are certainly different/more optimal ways to do this):

import javax.swing.Timer;

Date dt = new Date();

timer = new Timer(10, new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        int min = 0, sec = 0, hundredSec = 0;

        long millisec = (new Date()).getTime() - dt.getTime();
        hundredSec = ( millisec / 10 ) % 100;
        sec = ( millisec / 1000 ) % 60;
        min = ( millisec / 60000 );

        timerL.setText(String.format("%02d:%02d:%02d", min, sec, hundredSec));

    }
});

There are a couple issues here (missing timerL declaration, and I fixed the millisec reference in the setText call), but they're the same as you had above, so I assume you're just posting a snippet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.