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 final 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) {
22  		    throw new IllegalArgumentException();
23  		}
24  		
25  		long seconds = 0;
26          long minutes = 0;
27          long hours = 0;
28  
29          if (milliseconds > 1000) {
30              seconds = milliseconds / 1000;
31          }
32  
33          if (seconds > 60) {
34              minutes = seconds / 60;
35              seconds = seconds % 60;
36          }
37  
38          if (minutes > 60) {
39              hours = minutes / 60;
40              minutes = minutes % 60;
41          }
42  
43          StringBuilder res = new StringBuilder();
44          if (hours > 0) {
45              res.append(hours).append("h ");
46          }
47          if (hours > 0 || minutes > 0) {
48              res.append(minutes).append("m ");
49          }
50          res.append(seconds).append('s');
51          return res.toString();
52  	}
53  }