title: Groovy: TimeDuration - Custom Output Format
body: <p>I am trying to out the groovy time duration without the default field names, i.e. days, hours, minutes, seconds, etc. </p>
  
  <p>This formatted time difference is being put at the end of a progress bar so the real time run time of the process is displayed. </p>
  
  <pre><code>public static String calculateTime(Date timeStart)
  {
          Date timeStop = new Date()
          TimeDuration duration = TimeCategory.minus(timeStop, timeStart)
          return "${duration.days}:${duration.hours}:${duration.minutes}:${duration.seconds}".toString()
          //return duration.toString()
  }
  
  
  public static void printProgBar(long num, long den, Date startDate){
      def bar = "[";
          DecimalFormat df = new DecimalFormat("#.00");
          def dataType = "";
  
          def len = den.toString().length()
  
          if(den == 0)
          {
                  den = 1;
          }
  
          BigDecimal percent = ((num/den)*100)
  
          if(len &lt;= 3){ dataType = "bytes" }
          if(len &lt;= 6 &amp;&amp; len &gt; 3){ dataType = "Kbs" }
          if(len &lt;= 9 &amp;&amp; len &gt; 6){ dataType = "Mbs" }
          if(len &lt;= 12 &amp;&amp; len &gt; 9){ dataType = "Gbs" }
  
          def n = num
          def d = den
          if(dataType.equals("Kbs"))
          {
              n = num/1024
              d = den/1024
          }
          if(dataType.equals("Mbs"))
          {
              n = num/1024/1024
              d = den/1024/1024
          }
          if(dataType.equals("Gbs"))
          {
              n = num/1024/1024/1024
              d = den/1024/1024/1024
          }
  
      for(int i = 0; i &lt; 50; i++){
          if( i &lt; (percent/2)){
              bar += "|";
          }else if( i == (percent/2)){
              bar += "&gt;";
          }else{
              bar += " ";
          }
      }
  
      def message = (percent == 100) ? "Complete!" : "processed..."
  
      bar += "]   " + df.format(percent) + "%    " + df.format(n) + " of " + df.format(d) + " $dataType $message " + HSTools.calculateTime(startDate);
      print "\r" + bar;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <p>Loading data for: Patient_Contacts_Data.txt
  Creating Scanner and setting delimiter: \|^@*^@r^@*^@\|
  [||||||||||||||||||||||||||||||||||||||||||||||||||]   100.00%    1.60 of 1.60 Mbs Complete! 0:0:0:5656
  Records Processed: 5857
  Elapsed Time: 0:0:0:56</p>
  
  <p>For the most part my code appears to be working, however occasionally the seconds appear to double up on themselves.</p>
  
  <p>Any thoughts on what could be causing this? Or suggestions on how to better format this TimeDuration?</p>