2016-03-03 59 views
0

我創建了秒倒計時程序,不幸的是我不知道如何將其轉換爲hh:mm:ss格式。我想將它顯示爲hh:mm:ss到我分配給它的標籤上。只是爲了上下文,我有一個數據庫與這個程序,我可以從數據庫中拉秒。我想從數據庫中提取時間格式,但由於某種原因,它不起作用。如果你能幫助我,我真的很感激它。在Java中使用hh:mm:ss格式的倒計時定時器

這裏是我的代碼:

public class Event implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
      int count = (int)(Double.parseDouble(tf.getText())); \\tf is a label where I display the seconds int data from my database 
      timerLabel.setText(String.valueOf(count)); 
      TimeClass tc = new TimeClass(count); 
      timer = new Timer(1000, tc); 
      timer.start(); 
    } 
} 

public class TimeClass implements ActionListener { 
    int counter; 
    public TimeClass(int counter) { 
     this.counter= counter; 
    } 
    public void actionPerformed(ActionEvent tc) { 
     counter--; 
     if(counter >= 1) { 
       timerLabel.setText(String.valueOf(counter)); 
     } 
     else { 
       timer.stop(); 
       timerLabel.setText("Done!"); 
       Toolkit.getDefaultToolkit().beep(); 
     } 
    } 
} 

回答

0
public static void main(String[] args) throws ParseException { 
    long millis = 3600000; 
    String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), 
      TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), 
      TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 
    System.out.println(hms); 
}