title: Java How to get just HH:mm:ss from Date
body: <p>For my first Java application, I am making a stopwatch. The stopwatch displays time past as hours, minutes and seconds. Like so 00:00:00.</p>
  
  <p>I wish to display an Alarm (Alert box, whatever) once a certain amount of time (user defined) has past.</p>
  
  <p>Here is the code that generates the stopwatch display (ignore the deciseconds, we wont concern ourselves with that for the alarm):</p>
  
  <pre><code>    swChronometer = new Timer(10, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              int decisec = (int)(System.currentTimeMillis() - swWatchStart) / 10; // 10 x millisecond
              int seconds = decisec / 100;
              int days = seconds / 86400; // 86400 seconds in a day
              int hours = (seconds / 3600) - (days * 24); // 3600 seconds in a hour
              int min = (seconds / 60) - (days * 1440) - (hours * 60); // 60 seconds in a min
              int sec = seconds % 60;
              int decis = decisec % 100;
  
              //------------------------------------------------------- format the ints
              DecimalFormat formatter = new DecimalFormat("00");
              String hoursFormatted = formatter.format(hours);
              String minFormatted = formatter.format(min);
              String secFormatted = formatter.format(sec);
              String decisFormatted = formatter.format(decis);
  
              //------------------------------------------------------- update display
              String s = new String(""+hoursFormatted+":"+minFormatted+":"+secFormatted+"");
              String sDs = new String(decisFormatted);
              swDisplayTimeLabel.setText(s);
              swDisplayDeciseconds.setText(sDs);
          }        
      });
  </code></pre>
  
  <p>As you can see the Timer ints are eventually converted to strings and then displayed.</p>
  
  <p>There are radio buttons for After and Every - So an alarm after <em>n</em> amount of time, or every <em>n</em> amount of time.</p>
  
  <p>I have a JSpinner so the user can define the <em>n</em> time, that is formatted to show only 00:00:00:</p>
  
  <pre><code>    // ----------------------------------
      // set spinner format
      // ----------------------------------
      // inital date at 12pm 00:00:00
      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.HOUR_OF_DAY, 24); // 24 == 12 PM == 00:00:00
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
  
      swSpinnerTime.setValue(calendar.getTime());
  
      // format field
      JSpinner.DateEditor editor = new JSpinner.DateEditor(swSpinnerTime, "HH : mm : ss");           
      DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
      formatter.setAllowsInvalid(false); // sets it so user cant edit colon
      formatter.setOverwriteMode(true);
  
      swSpinnerTime.setEditor(editor);
  </code></pre>
  
  <p>There is also a combobox so the user can select what type of alarm.</p>
  
  <p>Whenever any of these inputs is acted on by the user my function is called to deal with it:</p>
  
  <pre><code>private void swAlarmSettings() {
      //------------------------------------------------------- which radio is selected
      String swRadioText = "";
  
      if(swRadioAfter.isSelected()) {
          swRadioText = swRadioAfter.getText();
      }
  
      if(swRadioEvery.isSelected()) {
          swRadioText = swRadioEvery.getText();
      }
  
      // debug
      System.out.println("Radio Selected: " +swRadioText);
  
      //------------------------------------------------------- which time is selected     
      Date swSpinnerTimeValue;  
      swSpinnerTimeValue = (Date)swSpinnerTime.getValue();
  
      // debug
      System.out.println("swSpinnerTimeValue: " +swSpinnerTimeValue);
  
      //------------------------------------------------------- which alarm is selected
      String swDropDownSelected = (String)swDropDown.getSelectedItem();
  
      // debug
      System.out.println("swDropDownSelected: " +swDropDownSelected);       
  }
  </code></pre>
  
  <p>However whenever this function is called it prints out the date as:</p>
  
  <pre><code>   swSpinnerTimeValue: Tue Oct 14 00:00:00 BST 2014
  </code></pre>
  
  <p>So this is where I'm stuck on how to proceed. I wasn't expecting the whole date (even though its wrong(today is the 13th Oct, not the 14th)) to be displayed when I already formatted it in the JSpinner.</p>
  
  <p>So I'm presuming I need to parse this value somehow, strip out the data I don't need so I'm left with just HH:mm:ss and then convert it to strings, so that I may compare it to my stopwatch display (String swDisplayTimeLabel) and then fire the alarm once they match.</p>
  
  <p>I hope I've made my query clear and provided enough info. Thanks for any help.</p>
  
  <p>I'm working in Netbeans and using the Swing design section to create my application.</p>