Calculate the elapsed time between two times specified in milliseconds. @param start The start of the time period @param end The end of the time period @return a string of the form "XhYmZs" when the elapsed time is X hours, Y minutes and Z seconds or null if start > end
(long start, long end)
| 35 | * minutes and Z seconds or null if start > end. |
| 36 | */ |
| 37 | public static String elapsedTime(long start, long end) { |
| 38 | if (start > end) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | long[] elapsedTime = new long[TIME_FACTOR.length]; |
| 43 | |
| 44 | for (int i = 0; i < TIME_FACTOR.length; i++) { |
| 45 | elapsedTime[i] = start > end ? -1 : (end - start) / TIME_FACTOR[i]; |
| 46 | start += TIME_FACTOR[i] * elapsedTime[i]; |
| 47 | } |
| 48 | |
| 49 | NumberFormat nf = NumberFormat.getInstance(Locale.ROOT); |
| 50 | nf.setMinimumIntegerDigits(2); |
| 51 | StringBuffer buf = new StringBuffer(); |
| 52 | for (int i = 0; i < elapsedTime.length; i++) { |
| 53 | if (i > 0) { |
| 54 | buf.append(":"); |
| 55 | } |
| 56 | buf.append(nf.format(elapsedTime[i])); |
| 57 | } |
| 58 | return buf.toString(); |
| 59 | } |
| 60 | } |