boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

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


avatar
作者 2025年9月16日 7

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免费学习笔记(深入)”;

<dependency>     <groupId>org.apache.commons</groupId>     <artifactId>commons-lang3</artifactId>     <version>3.12.0</version> <!-- 使用最新稳定版本 --> </dependency>

接下来,你可以直接在代码中使用formatDurationWords方法:

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

神笔马良

神笔马良 – AI让剧本一键成片。

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

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

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中的毫秒时间转换为人类友好的可读字符串。它避免了手动拼接的繁琐和潜在错误,是处理此类时间格式化需求的优选方案。



评论(已关闭)

评论已关闭