下面列出了org.apache.commons.lang.time.DurationFormatUtils#formatDuration ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected static String getQueueFirstMessageAgeAsString(Session jSession,
String queueName, long currentTime) {
try {
long age = getQueueFirstMessageAge(jSession, queueName, null,
currentTime, false);
if (age == -2) {
return "??";
} else if (age == -1) {
return null;
} else {
return DurationFormatUtils.formatDuration(age, "ddd-HH:mm:ss");
}
} catch (JMSException e) {
return "?";
}
}
public static String getAge(long value) {
long currentTime = (new Date()).getTime();
long age = currentTime - value;
String ageString = DurationFormatUtils.formatDuration(age, "d") + "d";
if ("0d".equals(ageString)) {
ageString = DurationFormatUtils.formatDuration(age, "H") + "h";
if ("0h".equals(ageString)) {
ageString = DurationFormatUtils.formatDuration(age, "m") + "m";
if ("0m".equals(ageString)) {
ageString = DurationFormatUtils.formatDuration(age, "s") + "s";
if ("0s".equals(ageString)) {
ageString = age + "ms";
}
}
}
}
return ageString;
}
@Override
public void onTick(long l) {
if (l > 0) {
mTimeStr = DurationFormatUtils.formatDuration(l, mTimePattern);
//这是apache中的common的lang包中DurationFormatUtils类中的formatDuration,通过传入
//一个时间格式就会自动将倒计时转换成相应的mTimePattern的样式(HH:mm:ss或dd天HH时mm分ss秒)
setBackgroundSpan(mTimeStr);
}
}
private String format(long avgDuration) {
String formatted = null;
if (avgDuration>0) {
int days = (int) (avgDuration / SECONDS_DAY);
long lessThanDay = (avgDuration % SECONDS_DAY);
formatted = days + ":" +
DurationFormatUtils.formatDuration(lessThanDay*1000, "HH:mm:ss:SS");
} else {
formatted = 0 + ":" +
DurationFormatUtils.formatDuration(0, "HH:mm:ss:SS");
}
return formatted;
}
private String format(long avgDuration) {
String formatted = null;
if (avgDuration>0) {
int days = (int) (avgDuration / SECONDS_DAY);
long lessThanDay = (avgDuration % SECONDS_DAY);
formatted = days + ":" +
DurationFormatUtils.formatDuration(lessThanDay*1000, "HH:mm:ss:SS");
} else {
formatted = 0 + ":" +
DurationFormatUtils.formatDuration(0, "HH:mm:ss:SS");
}
return formatted;
}
private String format(long avgDuration) {
String formatted = null;
if (avgDuration>0) {
int days = (int) (avgDuration / SECONDS_DAY);
long lessThanDay = (avgDuration % SECONDS_DAY);
formatted = days + ":" +
DurationFormatUtils.formatDuration(lessThanDay*1000, "HH:mm:ss:SS");
} else {
formatted = 0 + ":" +
DurationFormatUtils.formatDuration(0, "HH:mm:ss:SS");
}
return formatted;
}
private String format(long avgDuration) {
String formatted = null;
if (avgDuration>0) {
int days = (int) (avgDuration / SECONDS_DAY);
long lessThanDay = (avgDuration % SECONDS_DAY);
formatted = days + ":" +
DurationFormatUtils.formatDuration(lessThanDay*1000, "HH:mm:ss:SS");
} else {
formatted = 0 + ":" +
DurationFormatUtils.formatDuration(0, "HH:mm:ss:SS");
}
return formatted;
}
@Override
public void onTick(long l) {
if (l > 0) {
mTimeStr = DurationFormatUtils.formatDuration(l, mTimePattern);
//这是apache中的common的lang包中DurationFormatUtils类中的formatDuration,通过传入
//一个时间格式就会自动将倒计时转换成相应的mTimePattern的样式(HH:mm:ss或dd天HH时mm分ss秒)
setBackgroundSpan(mTimeStr);
}
}
public static String duration(Duration d) {
String result = null;
if (d != null) {
long ms = d.toMillis();
if (ms >= 0) {
result = DurationFormatUtils.formatDuration(ms, "HH:mm:ss");
}
else {
result = DurationFormatUtils.formatDuration(-ms, "-HH:mm:ss");
}
}
return result;
}
protected void updateTimestamp(long timestamp) {
if (timestamp == currentTime)
return;
currentTime = timestamp;
String formatDurationHMS = DurationFormatUtils.formatDuration(timestamp,
(timestamp == 0 ? "--:--:--.---" : "HH:mm:ss.SSS"), true);
timeLabel.setText(formatDurationHMS);
getParent().layout();
}
protected String getReadableSimulationTime(long timestamp) {
long days = TimeUnit.MILLISECONDS.toDays(timestamp);
long hours = TimeUnit.MILLISECONDS.toHours(timestamp);
long minutes = TimeUnit.MILLISECONDS.toMinutes(timestamp);
long seconds = TimeUnit.MILLISECONDS.toSeconds(timestamp);
return DurationFormatUtils.formatDuration(timestamp,
(days > 0 ? "dd 'days '" : "") + (hours > 0 ? "HH 'hours '" : "") + (minutes > 0 ? "mm 'minutes '" : "")
+ (seconds > 0 ? "ss 'seconds '" : ""),
false);
}
public static String getDuration(Date dateBegin, Date dateEnd) {
return DurationFormatUtils.formatDuration(dateEnd.getTime()-dateBegin.getTime(), "HH:mm", true);
}
private String convertTimeMs(long timeMs) {
if (timeMs < 1000) {
return Long.toString(timeMs) + " msec";
}
return DurationFormatUtils.formatDuration(timeMs, "HH:mm:ss") + " HH:MM:SS";
}
private String convertTimeMs(long timeMs) {
if (timeMs < 1000) {
return Long.toString(timeMs) + " msec";
}
return DurationFormatUtils.formatDuration(timeMs, "HH:mm:ss") + " HH:MM:SS";
}
private static String format(long miliseconds)
{
return DurationFormatUtils.formatDuration(miliseconds, "mm:ss");
}
public static String formatMsForLog(long ms) {
//return LOG_TIME_FORMAT.format(new Date(ms));
return DurationFormatUtils.formatDuration(ms, "H:mm:ss.S");
}