1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00

BetweenFormatter支持自定义设置单位

This commit is contained in:
Looly 2024-06-17 15:45:38 +08:00
parent d556f5b62f
commit 51c1588081
2 changed files with 4 additions and 7 deletions
CHANGELOG.md
hutool-core/src/main/java/cn/hutool/core/date

View File

@ -10,6 +10,7 @@
* 【core 】 CollUtil.subtract增加空判定issue#3605@Github
* 【core 】 优化DateUtil.format(Date date, String format)接口效率pr#1226@Gitee
* 【csv 】 CsvWriter.writeBeans增加重载可选是否写出表头issue#IA57W2@Gitee
* 【core 】 BetweenFormatter支持自定义设置单位pr#1228@Gitee
### 🐞Bug修复
* 【core 】 修复AnnotationUtil可能的空指针错误

View File

@ -19,10 +19,6 @@ import java.util.function.Function;
public class BetweenFormatter implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 单位格式化器
*/
public static Function<Level, String> DEFAULT_LEVEL_FORMATTER = (level) -> level.name;
/**
* 时长毫秒数
*/
@ -38,7 +34,7 @@ public class BetweenFormatter implements Serializable {
/**
* 格式化器
*/
private Function<Level, String> levelFormatter = DEFAULT_LEVEL_FORMATTER;
private Function<Level, String> levelFormatter = Level::getName;
/**
* 分隔符
*/
@ -86,7 +82,7 @@ public class BetweenFormatter implements Serializable {
final int level = this.level.ordinal();
int levelCount = 0;
if (isLevelCountValid(levelCount) && 0 != day && level >= Level.DAY.ordinal()) {
if (isLevelCountValid(levelCount) && day > 0) {
sb.append(day).append(levelFormatter.apply(Level.DAY)).append(separator);
levelCount++;
}
@ -172,7 +168,7 @@ public class BetweenFormatter implements Serializable {
* @return this
*/
public BetweenFormatter setSeparator(String separator) {
this.separator = separator == null ? StrUtil.EMPTY : separator;
this.separator = StrUtil.nullToEmpty(separator);
return this;
}