首页 > Java > java教程 > 正文

Java中将毫秒转换为可读字符串的简易方法

花韻仙語
发布: 2025-09-16 09:28:16
原创
809人浏览过

Java中将毫秒转换为可读字符串的简易方法

本文介绍如何使用Apache Commons Lang库中的DurationFormatUtils.formatDurationWords方法,将Java中的毫秒时间转换为人类友好的可读字符串,有效避免手动拼接,实现简洁高效的时间格式化。

java开发中,我们经常需要处理时间数据,尤其是在日志记录、用户界面展示或报告生成时,将原始的毫秒数转换为“x天y小时z分钟”这种人类可读的格式至关重要。传统的做法可能涉及复杂的数学运算来逐级计算天、小时、分钟和秒,然后手动拼接字符串,这不仅代码量大、易出错,而且难以处理诸如“0天”或“0秒”等零值元素的显示问题。为了解决这一痛点,apache commons lang库提供了一个极其方便的工具类durationformatutils。

使用DurationFormatUtils.formatDurationWords进行转换

Apache Commons Lang是一个功能丰富的Java工具库,其中org.apache.commons.lang3.time.DurationFormatUtils类专门用于处理持续时间的格式化。其核心方法formatDurationWords能够将毫秒数转换为描述性的字符串,并且提供了灵活的选项来控制输出。

核心方法详解

formatDurationWords方法签名如下:

public static String formatDurationWords(long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements)
登录后复制
  • durationMillis: 需要转换的毫秒数,类型为long。
  • suppressLeadingZeroElements: 一个布尔值。如果为true,则会抑制输出字符串中开头的零值元素。例如,如果持续时间不足一天,"0 days"将不会显示。
  • suppressTrailingZeroElements: 一个布尔值。如果为true,则会抑制输出字符串中末尾的零值元素。例如,如果持续时间恰好是整数小时,"0 minutes 0 seconds"将不会显示。

通常情况下,为了获得最简洁和用户友好的输出,我们会将suppressLeadingZeroElements和suppressTrailingZeroElements都设置为true。

示例代码

首先,确保你的项目中已引入Apache Commons Lang库。如果你使用Maven,可以在pom.xml中添加以下依赖:

立即学习Java免费学习笔记(深入)”;

秒哒
秒哒

秒哒-不用代码就能实现任意想法

秒哒 134
查看详情 秒哒
<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
登录后复制

从输出可以看出:

  • 当suppressLeadingZeroElements和suppressTrailingZeroElements都为true时,输出非常简洁,不会出现“0 days”、“0 minutes”等冗余信息。
  • formatDurationWords会自动处理单复数(例如“1 second” vs “2 seconds”)。
  • 当suppressLeadingZeroElements为false时,即使天数为0,也会显示"0 days"。

注意事项与总结

  • 依赖管理: 确保正确引入commons-lang3库,否则会遇到NoClassDefFoundError。
  • 版本兼容性: 建议使用较新版本的commons-lang3以获取最佳性能和功能。
  • 语言环境: formatDurationWords方法生成的字符串是英文的,不支持直接的本地化(如中文)。如果需要多语言支持,你可能需要在此方法的基础上进行二次封装,或者考虑使用更复杂的国际化框架。
  • 精度: 该方法以天、小时、分钟、秒为单位进行格式化,毫秒以下的精度会被忽略。

总而言之,DurationFormatUtils.formatDurationWords提供了一种极其简便、健壮且易于维护的方式,将Java中的毫秒时间转换为人类友好的可读字符串。它避免了手动拼接的繁琐和潜在错误,是处理此类时间格式化需求的优选方案。

以上就是Java中将毫秒转换为可读字符串的简易方法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号