
在java开发中,我们经常需要处理时间数据,尤其是在日志记录、用户界面展示或报告生成时,将原始的毫秒数转换为“x天y小时z分钟”这种人类可读的格式至关重要。传统的做法可能涉及复杂的数学运算来逐级计算天、小时、分钟和秒,然后手动拼接字符串,这不仅代码量大、易出错,而且难以处理诸如“0天”或“0秒”等零值元素的显示问题。为了解决这一痛点,apache commons lang库提供了一个极其方便的工具类durationformatutils。
Apache Commons Lang是一个功能丰富的Java工具库,其中org.apache.commons.lang3.time.DurationFormatUtils类专门用于处理持续时间的格式化。其核心方法formatDurationWords能够将毫秒数转换为描述性的字符串,并且提供了灵活的选项来控制输出。
formatDurationWords方法签名如下:
public static String formatDurationWords(long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements)
通常情况下,为了获得最简洁和用户友好的输出,我们会将suppressLeadingZeroElements和suppressTrailingZeroElements都设置为true。
首先,确保你的项目中已引入Apache Commons Lang库。如果你使用Maven,可以在pom.xml中添加以下依赖:
立即学习“Java免费学习笔记(深入)”;
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version> <!-- 使用最新稳定版本 -->
</dependency>接下来,你可以直接在代码中使用formatDurationWords方法:
import org.apache.commons.lang3.time.DurationFormatUtils;
public class MillisecondsConverter {
public static void main(String[] args) {
// 示例1: 5分钟2秒 (302000毫秒)
long milliseconds1 = 302000;
String readableString1 = DurationFormatUtils.formatDurationWords(milliseconds1, true, true);
System.out.println("302000 毫秒转换为: " + readableString1); // 预期输出: 5 minutes 2 seconds
// 示例2: 2小时 (7200000毫秒)
long milliseconds2 = 7200000;
String readableString2 = DurationFormatUtils.formatDurationWords(milliseconds2, true, true);
System.out.println("7200000 毫秒转换为: " + readableString2); // 预期输出: 2 hours
// 示例3: 1秒 (1000毫秒)
long milliseconds3 = 1000;
String readableString3 = DurationFormatUtils.formatDurationWords(milliseconds3, true, true);
System.out.println("1000 毫秒转换为: " + readableString3); // 预期输出: 1 second
// 示例4: 0毫秒
long milliseconds4 = 0;
String readableString4 = DurationFormatUtils.formatDurationWords(milliseconds4, true, true);
System.out.println("0 毫秒转换为: " + readableString4); // 预期输出: 0 seconds
// 示例5: 1天3小时45分钟12秒 (使用不同的抑制策略)
long milliseconds5 = (24 * 60 * 60 * 1000L) + (3 * 60 * 60 * 1000L) + (45 * 60 * 1000L) + (12 * 1000L);
String readableString5 = DurationFormatUtils.formatDurationWords(milliseconds5, true, true);
System.out.println("1天3小时45分钟12秒转换为 (全抑制): " + readableString5); // 预期输出: 1 day 3 hours 45 minutes 12 seconds
// 示例6: 1小时1分钟1秒 (不抑制开头的零元素)
long milliseconds6 = 3661000; // 1小时1分钟1秒
String readableString6 = DurationFormatUtils.formatDurationWords(milliseconds6, false, true);
System.out.println("1小时1分钟1秒转换为 (不抑制开头): " + readableString6); // 预期输出: 0 days 1 hour 1 minute 1 second
}
}运行上述代码,你将看到类似以下输出:
302000 毫秒转换为: 5 minutes 2 seconds 7200000 毫秒转换为: 2 hours 1000 毫秒转换为: 1 second 0 毫秒转换为: 0 seconds 1天3小时45分钟12秒转换为 (全抑制): 1 day 3 hours 45 minutes 12 seconds 1小时1分钟1秒转换为 (不抑制开头): 0 days 1 hour 1 minute 1 second
从输出可以看出:
总而言之,DurationFormatUtils.formatDurationWords提供了一种极其简便、健壮且易于维护的方式,将Java中的毫秒时间转换为人类友好的可读字符串。它避免了手动拼接的繁琐和潜在错误,是处理此类时间格式化需求的优选方案。
以上就是Java中将毫秒转换为可读字符串的简易方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号