- DurationFormatUtils类简介
- maven地址
- 构造方法 DurationFormatUtils()
- formatDurationHMS(long durationMillis)
- formatDurationISO(long durationMillis)
- formatDuration(long durationMillis, String format)
- formatDurationWords(long durationMillis,boolean suppressLeadingZeroElements,boolean suppressTrailingZeroElements)
- formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone)
1.1描述
1 /** 2 * <p>Duration formatting utilities and constants. The following table describes the tokens 3 * used in the pattern language for formatting. </p> 4 * <table border="1"> 5 * <tr><th>character</th><th>duration element</th></tr> 6 * <tr><td>y</td><td>years</td></tr> 7 * <tr><td>M</td><td>months</td></tr> 8 * <tr><td>d</td><td>days</td></tr> 9 * <tr><td>H</td><td>hours</td></tr> 10 * <tr><td>m</td><td>minutes</td></tr> 11 * <tr><td>s</td><td>seconds</td></tr> 12 * <tr><td>S</td><td>milliseconds</td></tr> 13 * </table> 14 * 15 * @since 2.1 16 * @version $Id: DurationFormatUtils.java 1144993 2011-07-11 00:51:16Z ggregory $ 17 */ 18 public class DurationFormatUtils {...}
以上是DurationFormatUtils类源码中的描述,从中可见DurationFormatUtils是持续时间格式化工具和常量类。在格式化过程中使用的pattern如下
字符 | 所代表含义 |
y |
年 |
M |
月 |
d |
天 |
H |
小时 |
m |
分钟 |
s |
秒 |
S |
毫秒 |
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
2.方法
/** * <p>在常规编程中DurationFormatUtils不应该被实例化.</p>
* <p>这个public的构造方法是为了给其他工具的集成准备的.</p> */ public DurationFormatUtils() { super(); }
2.2 formatDurationHMS(long durationMillis)
/** * <p>Formats the time gap as a string.</p> * * <p>The format used is ISO8601-like: * <i>H</i>:<i>m</i>:<i>s</i>.<i>S</i>.</p> * * @param durationMillis the duration to format * @return the formatted duration, not null */ public static String formatDurationHMS(long durationMillis) { return formatDuration(durationMillis, "H:mm:ss.SSS"); }
2.2.1方法描述:
将时间间隔格式化为字符串,参数列表是一个long类型的毫秒数,格式化后的结果格式为H:mm:ss.SSS 时:分:秒.毫秒
2.2.2方法测试:
public static void testFormatDurationHMS(){ long timeGap = 360000;//36000毫秒 String formatResult = DurationFormatUtils.formatDurationHMS(timeGap); System.out.println(formatResult); }
2.2.3运行结果:
0:06:00.000
2.3 formatDurationISO(long durationMillis)
/** * <p>Formats the time gap as a string.</p> * * <p>The format used is the ISO8601 period format.</p> * * <p>This method formats durations using the days and lower fields of the * ISO format pattern, such as P7D6TH5M4.321S.</p> * * @param durationMillis the duration to format * @return the formatted duration, not null */ public static String formatDurationISO(long durationMillis) { return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false); }
2.3.1方法描述
将时间间隔格式化为字符串,参数列表是一个long类型的毫秒数,格式化所使用的格式为ISO8601
2.3.2方法测试
public static void testFormatDurationISO(){ long timeGap = 360000;//36000毫秒 String formatResult = DurationFormatUtils.formatDurationISO(timeGap); System.out.println(formatResult); }
2.3.3运行结果
P0Y0M0DT0H6M0.000S
2.4 formatDuration(long durationMillis, String format)
/** * <p>格式化时间间隔为字符串,使用指定的格式,并且自动填充0,使用默认的时区</p> * <p>此方法格式化时间间隔只能格式化为天或者更低级别(时,分,秒...),月份或者更高级别的不可用</p> * @param 毫秒数 * @param 格式化后的格式 * @return the formatted duration, not null */ public static String formatDuration(long durationMillis, String format) { return formatDuration(durationMillis, format, true); }
/** * 方法重载,多了个参数boolean padWithZeros * 为true,则会在格式化前自动加0 例如:格式化格式为dd,格式化后为1天,则自动填充为01天,否则不会自动填充 */ public static String formatDuration(long durationMillis, String format, boolean padWithZeros) {...}
2.4.1方法测试
public static void testFormatDuration(){ long timeGap = 3600000000l; String dayFormatPattern = "当前时间段月数:ddd天"; System.out.println(DurationFormatUtils.formatDuration(timeGap,dayFormatPattern)); System.out.println(DurationFormatUtils.formatDuration(timeGap,dayFormatPattern,false)); String monthFormatPattern = "当前时间段月数:M月"; System.out.println(DurationFormatUtils.formatDuration(timeGap,monthFormatPattern)); }
2.4.2运行结果
当前时间段月数:041天
当前时间段月数:41天
当前时间段月数:0月
2.5 formatDurationWords(long durationMillis,boolean suppressLeadingZeroElements,boolean suppressTrailingZeroElements)
/** * <p>将时间间隔格式化为字符串.</p> * * <p>只适用于天或者以下</p> * * @param durationMillis 时间段,毫秒 * @param suppressLeadingZeroElements 是否禁止结果前加0(,英文不行,翻译不准,看运行结果) * @param suppressTrailingZeroElements 是否禁止在结果后加0(看运行结果) * @return the formatted text in days/hours/minutes/seconds, not null */ public static String formatDurationWords( long durationMillis, boolean suppressLeadingZeroElements, boolean suppressTrailingZeroElements) {}
2.5.1测试
public static void testFormatDurationWords(){ long timeGap = 360000l; System.out.println(DurationFormatUtils.formatDurationWords(timeGap,true,true)); System.out.println(DurationFormatUtils.formatDurationWords(timeGap,false,true)); long timeGap2 = 360300l; System.out.println(DurationFormatUtils.formatDurationWords(timeGap2,false,true)); System.out.println(DurationFormatUtils.formatDurationWords(timeGap2,false,false)); }
2.5.2运行结果
//整体结果为 : 0 days 0 hours 6 minutes 0 seconds 6 minutes //前禁止,后禁止 0 days 0 hours 6 minutes //前不禁止,后禁止 0 days 0 hours 6 minutes //前不禁止,后禁止 0 days 0 hours 6 minutes 0 seconds //前不禁止,后不禁止
2.6 formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone)
/** * <p>使用指定的pattern格式化时间间隔. * 可选择是否自动在数值前填充0 * timezone 可选. </p> * @param startMillis 开始时间毫秒 * @param endMillis 结束时间毫秒 * @param format 格式化pattern * @param padWithZeros 是否填充0 * @param timezone 时区 * @return the formatted duration, not null */ public static String formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone) {...}
2.6.1方法测试
public static void testFormatPeriod(){ long timeGap1 = 0l; long timeGap2 = 31536001000l;//一年一秒 String formatPattern = "yyyy:MM:dd HH:mm:ss"; System.out.println(DurationFormatUtils.formatPeriod(timeGap1,timeGap2,formatPattern,false,TimeZone.getDefault())); System.out.println(DurationFormatUtils.formatPeriod(timeGap1,timeGap2,formatPattern,true,TimeZone.getDefault())); }
2.6.2运行结果
1:0:0 0:0:1 0001:00:00 00:00:01
2.6.3重载||内部调用
public static String formatPeriod(long startMillis, long endMillis, String format) { return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault()); } public static String formatPeriodISO(long startMillis, long endMillis) { return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault()); }