
在java开发中,我们经常需要处理时间数据,尤其是在表示持续时间(duration)时。原始的毫秒值(long类型)对于用户来说并不直观,例如3600000毫秒远不如“1小时”来得清晰。传统的做法是手动计算天、小时、分钟、秒,然后通过字符串拼接来构建可读的持续时间字符串。这种方法不仅代码量大,而且需要处理各种边界情况,如零值单位的省略(例如,“2小时0分钟0秒”简化为“2小时”),这使得代码变得复杂且易出错。
引入Apache Commons Lang的DurationFormatUtils
为了解决这一痛点,我们可以借助Apache Commons Lang库中的DurationFormatUtils工具类。该工具提供了一系列方法来格式化持续时间,其中formatDurationWords方法尤其适用于生成自然语言描述的持续时间字符串,并且能够智能地处理前导和尾随零元素的抑制。
1. 添加依赖
首先,你需要在项目的构建配置中添加Apache Commons Lang的依赖。如果你使用Maven,请在pom.xml中添加:
org.apache.commons commons-lang3 3.12.0
如果你使用Gradle,请在build.gradle中添加:
implementation 'org.apache.commons:commons-lang3:3.12.0' // 请使用最新稳定版本
2. 使用 formatDurationWords 方法
DurationFormatUtils.formatDurationWords 方法的签名如下:
立即学习“Java免费学习笔记(深入)”;
public static String formatDurationWords(long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements)
参数说明:
- durationMillis: 需要格式化的持续时间,以毫秒为单位的long值。
- suppressLeadingZeroElements: 如果为true,则会抑制前导的零值单位。例如,如果持续时间不足一天,则不会显示“0天”。
- suppressTrailingZeroElements: 如果为true,则会抑制尾随的零值单位。例如,如果持续时间刚好是2小时,则不会显示“0分钟0秒”。
示例代码:
下面通过几个例子展示如何使用formatDurationWords方法:
import org.apache.commons.lang3.time.DurationFormatUtils;
public class DurationFormatter {
public static void main(String[] args) {
// 1. 刚好是2小时
long duration1 = 2 * 60 * 60 * 1000L; // 7200000 毫秒
String result1 = DurationFormatUtils.formatDurationWords(duration1, true, true);
System.out.println("2小时: " + result1); // 输出: 2 hours
// 2. 5分钟2秒
long duration2 = (5 * 60 + 2) * 1000L; // 302000 毫秒
String result2 = DurationFormatUtils.formatDurationWords(duration2, true, true);
System.out.println("5分钟2秒: " + result2); // 输出: 5 minutes 2 seconds
// 3. 1天3小时15分钟
long duration3 = (24 * 60 * 60 + 3 * 60 * 60 + 15 * 60) * 1000L;
String result3 = DurationFormatUtils.formatDurationWords(duration3, true, true);
System.out.println("1天3小时15分钟: " + result3); // 输出: 1 day 3 hours 15 minutes
// 4. 不足1秒
long duration4 = 500L; // 500毫秒
String result4 = DurationFormatUtils.formatDurationWords(duration4, true, true);
System.out.println("不足1秒: " + result4); // 输出: 0 seconds (因为最短单位是秒,不足1秒会显示0秒)
// 5. 持续时间很长
long duration5 = (365 * 24 * 60 * 60 + 10 * 60 * 60 + 30 * 60 + 45) * 1000L; // 大约1年10小时30分钟45秒
String result5 = DurationFormatUtils.formatDurationWords(duration5, true, true);
System.out.println("长时间: " + result5); // 输出: 365 days 10 hours 30 minutes 45 seconds
}
}输出结果:
2小时: 2 hours 5分钟2秒: 5 minutes 2 seconds 1天3小时15分钟: 1 day 3 hours 15 minutes 不足1秒: 0 seconds 长时间: 365 days 10 hours 30 minutes 45 seconds
从输出可以看出,formatDurationWords方法自动处理了单位的转换和零元素的抑制,使得输出结果非常符合人类阅读习惯。它默认使用的单位是天、小时、分钟和秒。
3. 注意事项与总结
- 单位精度: formatDurationWords方法默认的最小单位是秒。如果你的持续时间非常短(例如只有几百毫秒),它会显示为“0 seconds”。如果你需要更细粒度的显示(如毫秒),则需要使用formatDuration方法并自定义格式,或者手动处理。
- 语言环境: formatDurationWords生成的字符串是英文的(如"days", "hours", "minutes", "seconds")。如果你的应用需要支持多语言环境下的持续时间显示,你可能需要结合国际化(i18n)资源文件来构建相应的本地化字符串,或者考虑其他更专业的国际化时间库。
- 灵活性: DurationFormatUtils还提供了其他方法,例如formatDuration(long durationMillis, String format),允许你通过自定义格式字符串(如"HH:mm:ss")来精确控制输出格式。然而,对于生成自然语言描述,formatDurationWords通常是最佳选择。
通过采用Apache Commons Lang的DurationFormatUtils.formatDurationWords方法,我们可以轻松地将原始的毫秒值转换为用户友好的时间字符串,显著简化了代码逻辑,提高了开发效率和应用的用户体验。这是一个在Java项目中处理持续时间格式化问题的强大且简洁的解决方案。










