简体   繁体   中英

Java countdown timer minutes and seconds

I have already written a 60 seconds countdown timer but I would like to transform this to have a minutes and seconds timer like mm:ss .

Can I rearrange this existing code to get that ?

Timer timer = new Timer();
TimerTask task = new TimerTask() {
    int i = 60;
    public void run(){
        if (i >= 0) {
            lab3.setText("Timer " + i--);
        }
    }
};
timer.scheduleAtFixedRate(task, 0, 1000);

You need to display remaining number of second i in format mimutes:seconds format. If you assume that there are always 60 seconds in a minute:

String time = String.format("%02d:%02d", i / 60, i % 60);
System.out.println(time); 

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