View Javadoc

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