title: Java: How to convert a string (HH:MM:SS) to a duration?
body: <p>i want to convert a string with a format of HH:MM:SS or MM:SS or SS into a datatype of Duration.</p>
  
  <p>solution:</p>
  
  <pre><code>    private ArrayList&lt;Duration&gt; myCdDuration = new ArrayList&lt;Duration&gt;();
  
          private void convert(String aDuration) {
  
              chooseNewDuration(stringToInt(splitDuration(aDuration))); //stringToInt() returns an int[] and splitDuration() returns a String[]
          }
  
          private void chooseNewDuration(int[] array) {
              int elements = array.length;
              switch (elements) {
              case 1:
                  myCdDuration.add(newDuration(true, 0, 0, 0, 0, 0, array[0]));
                  break;
              case 2:
                  myCdDuration.add(newDuration(true, 0, 0, 0, 0, array[0], array[1]));
                  break;
              case 3:
                  myCdDuration.add(newDuration(true, 0, 0, 0, array[0], array[1],
                          array[2]));
                  break;
              }
          }
  </code></pre>
  
  <p>thanks for help ...  any easier way to do that ? -> create your own Duration class:</p>
  
  <pre><code>public class Duration {
          private int intSongDuration;
          private String printSongDuration;
  
          public String getPrintSongDuration() {
              return printSongDuration;
          }
  
          public void setPrintSongDuration(int songDuration) {
              printSongDuration = intToStringDuration(songDuration);
          }
  
          public int getIntSongDuration() {
              return intSongDuration;
          }
  
          public void setIntSongDuration(int songDuration) {
              intSongDuration = songDuration;
          }
  
          public Duration(int songDuration) {
  
              setIntSongDuration(songDuration);
          }
  </code></pre>
  
  <p>Converts the int value into a String for output/print:</p>
  
  <pre><code>private String intToStringDuration(int aDuration) {
      String result = "";
  
      int hours = 0, minutes = 0, seconds = 0;
  
      hours = aDuration / 3600;
      minutes = (aDuration - hours * 3600) / 60;
      seconds = (aDuration - (hours * 3600 + minutes * 60));
  
      result = String.format("%02d:%02d:%02d", hours, minutes, seconds);
      return result;
  }
  </code></pre>