Hi, I have a problem where I want to get rid of the last digit a timer displays, which I'm not sure why it's displaying. As you can see from the runnable code below, it's adding on another random digit at the end of the time and I'm not sure why. How could I get rid of this last digit?
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SpringLayout; import layout.NameOfClass; public class NameOfClass extends JPanel implements ActionListener{ protected static JButton b1; static JTextField textField2; static JFrame frame = new JFrame("Frame"); //Start timer setup: public static long startTime = 0; public static long stopTime = 0; public static boolean running = false; public void start() { this.startTime = System.currentTimeMillis(); this.running = true; } public void stop() { this.stopTime = System.currentTimeMillis(); this.running = false; } public long getElapsedTime() { long elapsed; if (running) { elapsed = (System.currentTimeMillis() - startTime); } else { elapsed = (stopTime - startTime); } return elapsed; } public long getElapsedTimeSecs() { long elapsed; if (running) { elapsed = ((System.currentTimeMillis() - startTime) / 1000); } else { elapsed = ((stopTime - startTime) / 1000); } return elapsed; } //End timer setup public static void main (String args[]) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGame(); } }); } public static void createAndShowGame() { textField2 = new JTextField("00:00:000"); textField2.setEditable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); SpringLayout layout = new SpringLayout(); contentPane.setLayout(layout); b1 = new JButton("Start"); b1.setVerticalTextPosition(AbstractButton.CENTER); b1.setHorizontalTextPosition(AbstractButton.LEADING); b1.setMnemonic(KeyEvent.VK_S); try { b1.addActionListener(new NameOfClass()); } catch (Exception e) {} contentPane.add(textField2); contentPane.add(b1); //SET HEIGHT/WIDTH layout.putConstraint(SpringLayout.WEST, textField2, 10, SpringLayout.WEST, contentPane); layout.putConstraint(SpringLayout.NORTH, textField2, 242, SpringLayout.NORTH, contentPane); //END SET HEIGHT/WIDTH //SET HEIGHT/WIDTH layout.putConstraint(SpringLayout.WEST, b1, 197, SpringLayout.WEST, contentPane); layout.putConstraint(SpringLayout.NORTH, b1, 262, SpringLayout.NORTH, contentPane); //END SET HEIGHT/WIDTH frame.pack(); frame.setSize(458, 325); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { try{ final NameOfClass s = new NameOfClass(); final SimpleDateFormat TIME = new SimpleDateFormat("mm:ss:SS"); javax.swing.Timer t = new javax.swing.Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e) { textField2.setText(TIME.format(new Date(s.getElapsedTime()))); } }); if(e.getSource()==b1) { s.start(); t.start(); } }catch(Exception e1){} } }