title: Java Countdown Timer
body: <p>Hi I am trying to create a countdown stopwatch timer for my gym.</p>
  
  <p>Here is what I have for the actual displaying and decrementing of the time: (bear in mind I used my normal stopwatch one and just edited it briefly to decrement. It is still first pass and I still want to thread it all at some stage but for now this should work. )</p>
  
  <pre><code>            new Timer(1,new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  int seconds = (int)(System.currentTimeMillis()-watchStart)/1000;
                  int days = seconds / 86400;
     //these are just initialization values for testing
                              int startHour, startMin, startSec;
                              startHour = 5;
                              startMin = 5;
                              startSec = 0;
                  int hours = (seconds / 3600) - (days * 24);
                  int min = (seconds / 60) - (days * 1440) - (hours * 60);
                  int sec = seconds % 60;
                                      String s = String.format("%02d:%02d:%02d", startHour - hours, startMin - min, ((startSec == 0) ? startSec = 60 : startSec) - sec );
  
                  displayTimeLabel.setText(s);
              }
      });
  </code></pre>
  
  <p>Now my issue is:</p>
  
  <p>a) It is not decrementing the minute to start with (if it starts at say 5 minutes) </p>
  
  <p>b) If I start it at 30 seconds it will go into negative until it reaches a full minute then decrements the minute. ( I think I will need a method like if == 0 then minute - 1? but I fear that will break the actual count?)</p>
  
  <p>Thanks.</p>
  
  <p>Edit: Full program:</p>
  
  <pre><code>    package countdown;
  
      import java.awt.*;
      import static java.awt.Frame.MAXIMIZED_BOTH;
      import javax.swing.Timer;
      import javax.swing.*;
      import java.awt.event.*;
  
  
      public class CountDown extends JFrame implements ActionListener{
  
  
      private int hour;
      private int minute;
      private int second;
  // The component that shows the elapsed time.
  private JLabel displayTimeLabel;
  
  
  private long watchStart, watchEnd;
  
  
  private Timer theChronometer;
  
  // Keeps track of time when pausing the Timer.
  private long pausedTime;
  
  // Lets the program know if starting or resuming
  private boolean paused = false;
  
  // Button that changes from "Start" to "Resume" depending on pause status.
  private JButton activateTimerButton;
  
  // run the program
  public static void main(String[] args) {
  
      CountDown count = new CountDown();
      count.setVisible(true);
      count.setLocationRelativeTo(null);
  }
  
  
  public CountDown(){
  
      // initialize
      super();
      setExtendedState(MAXIMIZED_BOTH);
      setLayout(new BorderLayout());
  
  
      setLayout(new GridLayout(2,1)); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("CrossFit Slam");
      setBackground(Color.black);
      setForeground(Color.white);
  
  
  
      Font largeFontBOLD = new Font("Calibri", Font.BOLD,20);
      Font largeFontPLAIN = new Font("Calibri", Font.PLAIN,200);
  
      JPanel buttonPanel = new JPanel();
      buttonPanel.setLayout(new FlowLayout());
  
      activateTimerButton = new JButton("Start");// will display resume when the watch is paused
      JButton stopTimerButton = new JButton("Stop");
      JButton pauseTimerButton = new JButton("Pause");
  
      // register buttons to generate events when clicked
      activateTimerButton.addActionListener(this);
      stopTimerButton.addActionListener(this);
      pauseTimerButton.addActionListener(this);
  
      // the display for elapsed time
      displayTimeLabel = new JLabel("00:00:00");
      displayTimeLabel.setHorizontalAlignment(JLabel.CENTER);
      buttonPanel.setBackground(Color.black);
      buttonPanel.setForeground(Color.white);
  
      displayTimeLabel.setFont(largeFontPLAIN);
      displayTimeLabel.setForeground(Color.white);
  
      displayTimeLabel.setBackground(Color.black);
  
      activateTimerButton.setFont(largeFontBOLD);
      stopTimerButton.setFont(largeFontBOLD);
      pauseTimerButton.setFont(largeFontBOLD);
  
  displayTimeLabel.setOpaque(true);
  
  
      buttonPanel.add(activateTimerButton);
      buttonPanel.add(stopTimerButton);
      buttonPanel.add(pauseTimerButton);
  
      add(displayTimeLabel);
      add(buttonPanel, BorderLayout.PAGE_END);
  
  
      theChronometer =
      new Timer(1,new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  int seconds = (int)(System.currentTimeMillis()-watchStart)/1000;
                  int days = seconds / 86400;
                              int startHour, startMin, startSec;
                              startHour = 5;
                              startMin = 5;
                              startSec = 0;
                  int hours = (seconds / 3600) - (days * 24);
                  int min = (seconds / 60) - (days * 1440) - (hours * 60);
                  int sec = seconds % 60;
                                      String s = String.format("%02d:%02d:%02d", startHour - hours, startMin - min, ((startSec == 0) ? startSec = 60 : startSec) - sec );
  
                  displayTimeLabel.setText(s);
              }
      });
  }
  
  
  
  public void actionPerformed(ActionEvent e){
  
  
      if(e.getActionCommand().equals("Stop")){theChronometer.stop();}
  
  
      else if(e.getActionCommand().equals("Start") || e.getActionCommand().equals("Resume")){
          if(!paused){
             watchStart = System.currentTimeMillis();
             theChronometer.start();
          }
           else{
              watchStart = System.currentTimeMillis()+pausedTime;
              pausedTime = 0;
              theChronometer.start();
              paused = false;
              activateTimerButton.setText("Start");
           }
      }
  
  
      else if(e.getActionCommand().equals("Pause")){
          long now = System.currentTimeMillis();
          pausedTime -= (now - watchStart);
          theChronometer.stop();
          paused = true;
          activateTimerButton.setText("Resume");
      }
  }
  </code></pre>
  
  <p>}</p>