阅读目录
1.DurationFormatUtils类简介
1.1描述
1 /**
2 *
Duration formatting utilities and constants. The following table describes the tokens3 * used in the pattern language for formatting.
4 *character | duration element |
---|---|
y | years |
M | months |
d | days |
H | hours |
m | minutes |
s | seconds |
S | milliseconds |
18 public class DurationFormatUtils {...}
以上是DurationFormatUtils类源码中的描述,从中可见DurationFormatUtils是持续时间格式化工具和常量类。在格式化过程中使用的pattern如下
字符
所代表含义
y
年
M
月
d
天
H
小时
m
分钟
s
秒
S
毫秒
1.2maven地址
org.apache.commons
commons-lang3
3.7
2.方法
2.1构造方法 DurationFormatUtils()
/**
*
在常规编程中DurationFormatUtils不应该被实例化.
*这个public的构造方法是为了给其他工具的集成准备的.
*/publicDurationFormatUtils() { super(); }
2.2 formatDurationHMS(long durationMillis)
/***
Formats the time gap as a string.
*
*
The format used is ISO8601-like:
* H:m:s.S.
*
*@paramdurationMillis the duration to format
*@returnthe formatted duration, not null*/
public static String formatDurationHMS(longdurationMillis) {return formatDuration(durationMillis, "H:mm:ss.SSS");
}
2.2.1方法描述:
将时间间隔格式化为字符串,参数列表是一个long类型的毫秒数,格式化后的结果格式为H:mm:ss.SSS 时:分:秒.毫秒
2.2.2方法测试:
public static voidtestFormatDurationHMS(){ 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)
/***
Formats the time gap as a string.
*
*
The format used is the ISO8601 period format.
*
*
This method formats durations using the days and lower fields of the
* ISO format pattern, such as P7D6TH5M4.321S.
*
*@paramdurationMillis the duration to format
*@returnthe formatted duration, not null*/
public static String formatDurationISO(longdurationMillis) {return formatDuration(durationMillis, ISO_EXTENDED_FORMAT_PATTERN, false);
}
2.3.1方法描述
将时间间隔格式化为字符串,参数列表是一个long类型的毫秒数,格式化所使用的格式为ISO8601
2.3.2方法测试
public static voidtestFormatDurationISO(){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)
/***
格式化时间间隔为字符串,使用指定的格式,并且自动填充0,使用默认的时区
*
此方法格式化时间间隔只能格式化为天或者更低级别(时,分,秒...),月份或者更高级别的不可用
*@param毫秒数
*@param格式化后的格式
*@returnthe formatted duration, not null*/
public static String formatDuration(longdurationMillis, 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 voidtestFormatDuration(){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)
/***
将时间间隔格式化为字符串.
*
*
只适用于天或者以下
*
*@paramdurationMillis 时间段,毫秒
*@paramsuppressLeadingZeroElements 是否禁止结果前加0(,英文不行,翻译不准,看运行结果)
*@paramsuppressTrailingZeroElements 是否禁止在结果后加0(看运行结果)
*@returnthe formatted text in days/hours/minutes/seconds, not null*/
public staticString formatDurationWords(longdurationMillis,booleansuppressLeadingZeroElements,boolean suppressTrailingZeroElements) {}
2.5.1测试
public static voidtestFormatDurationWords(){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)
/***
使用指定的pattern格式化时间间隔.
* 可选择是否自动在数值前填充0
* timezone 可选.
*@paramstartMillis 开始时间毫秒
*@paramendMillis 结束时间毫秒
*@paramformat 格式化pattern
*@parampadWithZeros 是否填充0
*@paramtimezone 时区
*@returnthe formatted duration, not null*/
public static String formatPeriod(long startMillis, long endMillis, String format, boolean padWithZeros, TimeZone timezone) {...}
2.6.1方法测试
public static voidtestFormatPeriod(){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, longendMillis, String format) {return formatPeriod(startMillis, endMillis, format, true, TimeZone.getDefault());
}public static String formatPeriodISO(long startMillis, longendMillis) {return formatPeriod(startMillis, endMillis, ISO_EXTENDED_FORMAT_PATTERN, false, TimeZone.getDefault());
}