我创建了一个秒倒计时程序,不幸的是我不知道如何将其转换为 hh:mm:ss 格式。我想将其显示为 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();
}
}
}
最佳答案
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);
}
关于java - 带有 hh 的倒计时器 :mm:ss format in Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766339/