简体   繁体   中英

How to update time elapsed

I'm developing a program which will count the time until the button is clicked I'm having trouble with using Timer. I wanted to have something like 00:00:00 -> 00:00:01 -> 00:00:02 ... etc my problem is that

it is stuck in 00:00:01

here's my code

Timer timer=new Timer(1000,null);


JLabel time=new JLabel("00:00:00");
timer.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
         DecimalFormat df=new DecimalFormat("00");
         int h=0;
         int m=0;
         int s=0;
         s++;
         if(s==60)
         {
             m++;
             if(m==60)
             {
                  h++;
             }
         }
         time.setText(df.format(h)+":"+df.format(m)+":"+df.format(s));
         revalidate();
         repaint();
      }
   });
   timer.start();

You've declared your variables as local variables within the context of the ActionListener ...

@Override
public void actionPerformed(ActionEvent e)
{
     DecimalFormat df=new DecimalFormat("00");
     int h=0;
     int m=0;
     int s=0;

This means that each time actionPerformed the variables are reset to 0 ...

Try making them instance variables instead...

You're also not resetting the variables when they pass their limits, for example...

s++;
if (s >= 60) {
    s = 0;
    m++;
    if (m >= 60) {
        h++;
        m = 0;
    }
}

As an alternative, you could maintain a single counter, which acts as the number of seconds that have passed and use some module mathematics to calculate the time parts

private int count = 0;

//...

Timer timer = new Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        count++;

        int hours = count / (60 * 60);
        float remainder = count % (60 * 60);
        float mins = remainder / (60);
        remainder = remainder % (60);
        float seconds = remainder;

        DecimalFormat df=new DecimalFormat("00");
        time.setText(df.format(hours) + ":" + df.format(mins) + ":" + df.format(seconds));

    }
});
timer.start();

This makes it a little simpler in the fact that you are managing a single value and then deciding how best to format it, rather then managing three states...IMHO

If I understand you, you should first store the startTime -

final long startTime = System.currentTimeMillis();

Then use it to calculate your time fields in actionPerformed() ,

DecimalFormat df = new DecimalFormat("00");
long endTime = System.currentTimeMillis();
long diff = endTime - startTime;
int h = (int) (diff) / (60*60*1000);
diff -= h * (60*60*1000);
int m = (int) (endTime-startTime) / (60*1000);
diff -= m * (60 * 1000);
int s = (int) (diff / 1000);

time.setText(df.format(h) + ":" + df.format(m)
    + ":" + df.format(s));
revalidate();
repaint();

Edit

Based on your new requirements,

Replace

final long startTime = System.currentTimeMillis();

with

final Calendar startTime = Calendar.getInstance();

and then

DecimalFormat df = new DecimalFormat("00");
long endTime = System.currentTimeMillis();
long diff = endTime - startTime.getTimeInMillis();
int h = (int) (diff) / (60 * 60 * 1000);
diff -= h * (60 * 60 * 1000);
int m = (int) (endTime - startTime.getTimeInMillis()) / (60 * 1000);
diff -= m * (60 * 1000);
int s = (int) (diff / 1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM