View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util;
5   
6   /**
7    * 
8    * @author Brian Remedios
9    */
10  public class DateTimeUtil {
11  
12  	private DateTimeUtil() {}
13  	
14  	/**
15  	 * 
16  	 * @param milliseconds
17  	 * @return String
18  	 */
19  	public static String asHoursMinutesSeconds(long milliseconds) {
20  		
21  		if (milliseconds < 0) throw new IllegalArgumentException();
22  		
23  		long seconds = 0;
24          long minutes = 0;
25          long hours = 0;
26  
27          if (milliseconds > 1000) {
28              seconds = milliseconds / 1000;
29          }
30  
31          if (seconds > 60) {
32              minutes = seconds / 60;
33              seconds = seconds % 60;
34          }
35  
36          if (minutes > 60) {
37              hours = minutes / 60;
38              minutes = minutes % 60;
39          }
40  
41          StringBuilder res = new StringBuilder();
42          if (hours > 0) {
43              res.append(hours).append("h ");
44          }
45          if (hours > 0 || minutes > 0) {
46              res.append(minutes).append("m ");
47          }
48          res.append(seconds).append('s');
49          return res.toString();
50  	}
51  }