- 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()); }