如何将Timer的秒数转换为hh:mm:ss?

时间:2022-11-13 02:44:34

I am truely sorry if asking the same question twice is considered spamming as I already asked about backward timer a hour ago.

如果两次提出同样的问题被认为是垃圾邮件,我真的很抱歉,因为我已经在一小时前询问了反向计时器。

But now new problem with it is though I couldn't get anyone's attention to that question again. I have successfully coded the timer thanks to you guys but then I tried to convert seconds tot he hh:mm:ss format but it didn't work. Instead of continuosely going till it is 00:00:00. It just shows the time that I coded it and that's it.

但现在新的问题是,我再也无法引起任何人对这个问题的关注。我已经成功编码了计时器,感谢你们,但后来我试着将秒转换为hh:mm:ss格式,但它没有用。而不是一直持续到00:00:00。它只显示我编码的时间,就是这样。

Here's my code.

这是我的代码。

import java.util.Timer;
import java.util.TimerTask;
public class countdown extends javax.swing.JFrame {


public countdown() {
    initComponents();
    Timer timer;

    timer = new Timer();
    timer.schedule(new DisplayCountdown(), 0, 1000);
}

class DisplayCountdown extends TimerTask {

      int seconds = 5;
      int hr = (int)(seconds/3600);
      int rem = (int)(seconds%3600);
      int mn = rem/60;
      int sec = rem%60;
      String hrStr = (hr<10 ? "0" : "")+hr;
      String mnStr = (mn<10 ? "0" : "")+mn;
      String secStr = (sec<10 ? "0" : "")+sec; 

      public void run() {
           if (seconds > 0) {
              lab.setText(hrStr+ " : "+mnStr+ " : "+secStr+"");
              seconds--;
           } else {

              lab.setText("Countdown finished");
              System.exit(0);
          }    
    }
}     
public static void main(String args[]) {
    new countdown().setVisible(true);
}  

2 个解决方案

#1


16  

Move your calculations

移动你的计算

  int hr = seconds/3600;
  int rem = seconds%3600;
  int mn = rem/60;
  int sec = rem%60;
  String hrStr = (hr<10 ? "0" : "")+hr;
  String mnStr = (mn<10 ? "0" : "")+mn;
  String secStr = (sec<10 ? "0" : "")+sec; 

into the run method.

进入run方法。

#2


1  

public String getCountDownStringInMinutes(int timeInSeconds)
{
    return getTwoDecimalsValue(timeInSeconds/3600) + ":" + getTwoDecimalsValue(timeInSeconds/60) + ":" +     getTwoDecimalsValue(timeInSeconds%60);
}


public static String getTwoDecimalsValue(int value)
{
    if(value>=0 && value<=9)
    {
        return "0"+value;           
    }
    else
    {
        return value+"";
    }
}

#1


16  

Move your calculations

移动你的计算

  int hr = seconds/3600;
  int rem = seconds%3600;
  int mn = rem/60;
  int sec = rem%60;
  String hrStr = (hr<10 ? "0" : "")+hr;
  String mnStr = (mn<10 ? "0" : "")+mn;
  String secStr = (sec<10 ? "0" : "")+sec; 

into the run method.

进入run方法。

#2


1  

public String getCountDownStringInMinutes(int timeInSeconds)
{
    return getTwoDecimalsValue(timeInSeconds/3600) + ":" + getTwoDecimalsValue(timeInSeconds/60) + ":" +     getTwoDecimalsValue(timeInSeconds%60);
}


public static String getTwoDecimalsValue(int value)
{
    if(value>=0 && value<=9)
    {
        return "0"+value;           
    }
    else
    {
        return value+"";
    }
}