static String formatDuration(Token[] tokens, int years, int months, int days, int hours, int minutes, int seconds, int milliseconds, boolean padWithZeros) { StringBuffer buffer = new StringBuffer(); int sz = tokens.length; for(int i=0; i<sz; i++) { Token token = tokens[i]; Object value = token.getValue(); int count = token.getCount(); if(value instanceof StringBuffer) { buffer.append(value.toString()); } else { if(value == y) { buffer.append( padWithZeros ? StringUtils.leftPad(""+years, count, "0") : ""+years ); } else if(value == M) { buffer.append( padWithZeros ? StringUtils.leftPad(""+months, count, "0") : ""+months ); } else if(value == d) { buffer.append( padWithZeros ? StringUtils.leftPad(""+days, count, "0") : ""+days ); } else if(value == H) { buffer.append( padWithZeros ? StringUtils.leftPad(""+hours, count, "0") : ""+hours ); } else if(value == m) { buffer.append( padWithZeros ? StringUtils.leftPad(""+minutes, count, "0") : ""+minutes ); } else if(value == s) { buffer.append( padWithZeros ? StringUtils.leftPad(""+seconds, count, "0") : ""+seconds ); } else if(value == S) { buffer.append( padWithZeros ? StringUtils.leftPad(""+milliseconds, count, "0") : ""+milliseconds ); } } }  return buffer.toString();
public static String formatDuration(long durationMillis, String format) { return formatDuration(durationMillis, format, true);
    static String formatDuration(Token[] tokens, int years, int months, int days, int hours,                                  int minutes, int seconds, int milliseconds, boolean padWithZeros)     {         StringBuffer buffer = new StringBuffer();        int sz = tokens.length;        for(int i=0; i<sz; i++) {            Token token = tokens[i];            Object value = token.getValue();            int count = token.getCount();            if(value instanceof StringBuffer) {                buffer.append(value.toString());            } else {                if(value == y) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+years, count, "0") : ""+years );                 } else                if(value == M) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+months, count, "0") : ""+months );                 } else                if(value == d) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+days, count, "0") : ""+days );                 } else                if(value == H) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+hours, count, "0") : ""+hours );                 } else                if(value == m) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+minutes, count, "0") : ""+minutes );                 } else                if(value == s) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+seconds, count, "0") : ""+seconds );                 } else                if(value == S) {                    buffer.append( padWithZeros ? StringUtils.leftPad(""+milliseconds, count, "0") : ""+milliseconds );                 }            }        }                return buffer.toString();    }