BetweenFormatter显示方式增强

This commit is contained in:
Looly 2023-10-18 15:22:46 +08:00
parent d5c6a6d217
commit edada89d64
3 changed files with 88 additions and 27 deletions

View File

@ -42,15 +42,33 @@ public class BetweenFormatter implements Serializable {
* 格式化级别的最大个数
*/
private final int levelMaxCount;
/**
* 是否为简化模式此标记用于自定义是否输出各个位数中间为0的部分<br>
* 如为{@code true}输出 1小时3秒{@code false}输出 1小时0分3秒
*/
private boolean simpleMode = true;
/**
* 构造
* 创建 BetweenFormatter
*
* @param betweenMs 日期间隔
* @param level 级别按照天小时毫秒分为5个等级根据传入等级格式化到相应级别
* @return BetweenFormatter
*/
public BetweenFormatter(final long betweenMs, final Level level) {
this(betweenMs, level, 0);
public static BetweenFormatter of(final long betweenMs, final Level level) {
return of(betweenMs, level, 0);
}
/**
* 创建 BetweenFormatter
*
* @param betweenMs 日期间隔
* @param level 级别按照天小时毫秒分为5个等级根据传入等级格式化到相应级别
* @param levelMaxCount 格式化级别的最大个数假如级别个数为1但是级别到秒那只显示一个级别
* @return BetweenFormatter
*/
public static BetweenFormatter of(final long betweenMs, final Level level, final int levelMaxCount) {
return new BetweenFormatter(betweenMs, level, levelMaxCount);
}
/**
@ -85,23 +103,38 @@ public class BetweenFormatter implements Serializable {
final int level = this.level.ordinal();
int levelCount = 0;
if (isLevelCountValid(levelCount) && 0 != day) {
//
if (isLevelCountValid(levelCount) && day > 0) {
sb.append(day).append(Level.DAY.name);
levelCount++;
}
if (isLevelCountValid(levelCount) && 0 != hour && level >= Level.HOUR.ordinal()) {
sb.append(hour).append(Level.HOUR.name);
levelCount++;
//
if (isLevelCountValid(levelCount) && level >= Level.HOUR.ordinal()) {
if(hour >0 || (!this.simpleMode && StrUtil.isNotEmpty(sb))){
sb.append(hour).append(Level.HOUR.name);
levelCount++;
}
}
if (isLevelCountValid(levelCount) && 0 != minute && level >= Level.MINUTE.ordinal()) {
sb.append(minute).append(Level.MINUTE.name);
levelCount++;
//
if (isLevelCountValid(levelCount) && level >= Level.MINUTE.ordinal()) {
if(minute >0 || (!this.simpleMode && StrUtil.isNotEmpty(sb))){
sb.append(minute).append(Level.MINUTE.name);
levelCount++;
}
}
if (isLevelCountValid(levelCount) && 0 != second && level >= Level.SECOND.ordinal()) {
sb.append(second).append(Level.SECOND.name);
levelCount++;
//
if (isLevelCountValid(levelCount) && level >= Level.SECOND.ordinal()) {
if(second >0 || (!this.simpleMode && StrUtil.isNotEmpty(sb))){
sb.append(second).append(Level.SECOND.name);
levelCount++;
}
}
if (isLevelCountValid(levelCount) && 0 != millisecond && level >= Level.MILLISECOND.ordinal()) {
// 毫秒
if (isLevelCountValid(levelCount) && millisecond > 0 && level >= Level.MILLISECOND.ordinal()) {
sb.append(millisecond).append(Level.MILLISECOND.name);
// levelCount++;
}
@ -127,9 +160,11 @@ public class BetweenFormatter implements Serializable {
* 设置 时长毫秒数
*
* @param betweenMs 时长毫秒数
* @return this
*/
public void setBetweenMs(final long betweenMs) {
public BetweenFormatter setBetweenMs(final long betweenMs) {
this.betweenMs = betweenMs;
return this;
}
/**
@ -145,9 +180,23 @@ public class BetweenFormatter implements Serializable {
* 设置格式化级别
*
* @param level 格式化级别
* @return this
*/
public void setLevel(final Level level) {
public BetweenFormatter setLevel(final Level level) {
this.level = level;
return this;
}
/**
* 是否为简化模式此标记用于自定义是否输出各个位数中间为0的部分<br>
* 如为{@code true}输出 1小时3秒{@code false}输出 1小时0分3秒
*
* @param simpleMode 是否简化模式
* @return this
*/
public BetweenFormatter setSimpleMode(boolean simpleMode) {
this.simpleMode = simpleMode;
return this;
}
/**

View File

@ -1376,7 +1376,7 @@ public class DateUtil extends CalendarUtil {
* @return XX天XX小时XX分XX秒XX毫秒
*/
public static String formatBetween(final long betweenMs, final BetweenFormatter.Level level) {
return new BetweenFormatter(betweenMs, level).format();
return BetweenFormatter.of(betweenMs, level).format();
}
/**
@ -1387,7 +1387,7 @@ public class DateUtil extends CalendarUtil {
* @since 3.0.1
*/
public static String formatBetween(final long betweenMs) {
return new BetweenFormatter(betweenMs, BetweenFormatter.Level.MILLISECOND).format();
return BetweenFormatter.of(betweenMs, BetweenFormatter.Level.MILLISECOND).format();
}
// endregion
@ -1660,7 +1660,7 @@ public class DateUtil extends CalendarUtil {
int result = 0;
for (int i = lastIndex; i >= 0; i--) {
result += Integer.parseInt(hms.get(i)) * Math.pow(60, (lastIndex - i));
result += (int) (Integer.parseInt(hms.get(i)) * Math.pow(60, (lastIndex - i)));
}
return result;
}

View File

@ -20,27 +20,39 @@ public class BetweenFormatterTest {
@Test
public void formatTest(){
final long betweenMs = DateUtil.betweenMs(DateUtil.parse("2017-01-01 22:59:59"), DateUtil.parse("2017-01-02 23:59:58"));
final BetweenFormatter formater = new BetweenFormatter(betweenMs, BetweenFormatter.Level.MILLISECOND, 1);
Assertions.assertEquals(formater.toString(), "1天");
final BetweenFormatter formatter = new BetweenFormatter(betweenMs, BetweenFormatter.Level.MILLISECOND, 1);
Assertions.assertEquals(formatter.toString(), "1天");
}
@Test
public void formatBetweenTest(){
final long betweenMs = DateUtil.betweenMs(DateUtil.parse("2018-07-16 11:23:19"), DateUtil.parse("2018-07-16 11:23:20"));
final BetweenFormatter formater = new BetweenFormatter(betweenMs, BetweenFormatter.Level.SECOND, 1);
Assertions.assertEquals(formater.toString(), "1秒");
final BetweenFormatter formatter = new BetweenFormatter(betweenMs, BetweenFormatter.Level.SECOND, 1);
Assertions.assertEquals(formatter.toString(), "1秒");
}
@Test
public void formatBetweenTest2(){
final long betweenMs = DateUtil.betweenMs(DateUtil.parse("2018-07-16 12:25:23"), DateUtil.parse("2018-07-16 11:23:20"));
final BetweenFormatter formater = new BetweenFormatter(betweenMs, BetweenFormatter.Level.SECOND, 5);
Assertions.assertEquals(formater.toString(), "1小时2分3秒");
final BetweenFormatter formatter = new BetweenFormatter(betweenMs, BetweenFormatter.Level.SECOND, 5);
Assertions.assertEquals(formatter.toString(), "1小时2分3秒");
}
@Test
public void formatTest2(){
final BetweenFormatter formater = new BetweenFormatter(584, BetweenFormatter.Level.SECOND, 1);
Assertions.assertEquals(formater.toString(), "0秒");
final BetweenFormatter formatter = new BetweenFormatter(584, BetweenFormatter.Level.SECOND, 1);
Assertions.assertEquals(formatter.toString(), "0秒");
}
@Test
void issueI88LB8Test() {
String s = BetweenFormatter.of(3609000, BetweenFormatter.Level.SECOND).setSimpleMode(false).format();
Assertions.assertEquals("1小时0分9秒", s);
s = BetweenFormatter.of(9000, BetweenFormatter.Level.MILLISECOND).setSimpleMode(false).format();
Assertions.assertEquals("9秒", s);
s = BetweenFormatter.of(3600000, BetweenFormatter.Level.MILLISECOND).setSimpleMode(false).format();
Assertions.assertEquals("1小时0分0秒", s);
}
}