Problems with a simple stop watch program

I would appreciate help sorting out a problem with a simple stop watch program. The problem is that it throws up inappropriate values. For example, the first time I ran it today it showed the best time at 19 seconds before the actual time had reached 2 seconds. I restarted the program and it ran correctly until about the thirtieth time I started it again when it was going okay until the display suddenly changed to something like '-50:31:30:50-'. I don't have screenshot because I had twenty thirteen year olds suddenly yelling at me that it was wrong. I clicked 'Stop' and then 'Start' again and it ran correctly.
I have posted the whole code (minus the GUI section) because I want you to see that the program is very, very simple. I can't see where it could go wrong. I would appreciate any hints.
public class StopWatch extends javax.swing.JFrame implements Runnable {
    int startTime, stopTime, totalTime, bestTime;
    private volatile Thread myThread = null;
    /** Creates new form StopWatch */
    public StopWatch() {
     startTime = 0;
     stopTime = 0;
     totalTime = 0;
     bestTime = 0;
     initComponents();
    public void run() {
     Thread thisThread = Thread.currentThread();
     while(myThread == thisThread) {
         try {
          Thread.sleep(100);
          getEnd();
         } catch (InterruptedException e) {}
    public void start() {
     if(myThread == null) {
         myThread = new Thread(this);
         myThread.start();
    public void getStart() {
     Calendar now = Calendar.getInstance();
     startTime = (now.get(Calendar.MINUTE) * 60) + now.get(Calendar.SECOND);
    public void getEnd() {
     Calendar now1 = Calendar.getInstance();
     stopTime = (now1.get(Calendar.MINUTE) * 60) + now1.get(Calendar.SECOND);
     totalTime = stopTime - startTime;
     setLabel();
     if(bestTime < totalTime) bestTime = totalTime;
    public void setLabel() {
     if((totalTime % 60) < 10) {
         jLabel1.setText(""+totalTime/60+ ":0"+(totalTime % 60));
     } else {
         jLabel1.setText(""+totalTime/60 + ":"+(totalTime % 60));
     if((bestTime % 60) < 10) {
         jLabel3.setText(""+bestTime/60+ ":0"+(bestTime % 60));
     } else {
         jLabel3.setText(""+bestTime/60 + ":"+(bestTime % 60));
    private void ButtonClicked(java.awt.event.ActionEvent evt) {                              
     JButton temp = (JButton) evt.getSource();
     if(temp.equals(jButton1)) {
         start();
         getStart();
     if(temp.equals(jButton2)) {
         getEnd();
         myThread = null;
     * @param args the command line arguments
    public static void main(String args[]) {
     java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
          new StopWatch().setVisible(true);
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                  
}
Advertisement
Reply

Although I appreciate this information, it still doesn't actually solve the problem. I can't see an error in the logic (or the code). The fact that the formatting works most of the time suggests that the problem does not lie there. As well, I use the same basic code for other time related displays e.g. countdown timers where the user sets a maximum time and the computer stops when zero is reached. I haven't had an error is these programs.
For me, it is such a simple program and the room for errors seem small. I am guessing that I have misunderstood something about dates, but I obviously don't know.
Again, thank you for taking the time to look at the problem and post a reply.