4

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.

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);
}  
User
  • 4,023
  • 4
  • 37
  • 63
Hashan Wijetunga
  • 181
  • 2
  • 3
  • 9
  • 3
    is this programming by crowd sourcing? – Mitch Wheat Oct 06 '13 at 06:04
  • `int seconds = 5; int hr = (int)(seconds/3600);` ? what do you expect hour to be ? Try searching for this site for answer first: [how to convert milliseconds to hhmmss-format](http://stackoverflow.com/questions/9027317/how-to-convert-milliseconds-to-hhmmss-format) – Sage Oct 06 '13 at 06:08
  • No. I'm creating a whole software and timer is only a part of it and it's a part I'm don't quite understand due I'm being a beginner to Java language. – Hashan Wijetunga Oct 06 '13 at 06:09
  • hr is hour. I want it to convert according the seconds I give. Fr now I only give 5 secs but in final software I have to make it 3600 secs thus 1 hour. – Hashan Wijetunga Oct 06 '13 at 06:10
  • `lab.setText(hrStr+ " : "+mnStr+ " : "+secStr+"");` In the while loop you are not updating hour, minutes with `seconds--`; I have linked a page to my comment. Try using the solution to format time. – Sage Oct 06 '13 at 06:15
  • how do I make lab.setText(hrStr+ " : "+mnStr+ " : "+secStr+""); Update with the loop too? – Hashan Wijetunga Oct 06 '13 at 06:23
  • possible duplicate of [How to format a duration in java? (e.g format H:MM:SS)](http://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss) – Boann Oct 06 '13 at 06:29

2 Answers2

17

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.

Origineil
  • 3,108
  • 2
  • 12
  • 17
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+"";
    }
}
Piyush
  • 2,040
  • 16
  • 14