This commit is contained in:
Looly 2020-07-06 15:18:09 +08:00
parent 2fd5c8c32e
commit 48d67f0859
6 changed files with 112 additions and 47 deletions

View File

@ -9,6 +9,7 @@
* 【core 】 DateUtil增加formatChineseDatepr#932@Github
* 【core 】 ArrayUtil.isEmpty修改逻辑pr#948@Github
* 【core 】 增强StrUtil中空判断后返回数据性能pr#949@Github
* 【core 】 deprecate掉millsecond改为millisecondissue#I1M9P8@Github
### Bug修复
* 【core 】 修复NumberUtil.partValue有余数问题issue#I1KX66@Gitee

View File

@ -1,26 +1,33 @@
package cn.hutool.core.date;
import java.io.Serializable;
import cn.hutool.core.util.StrUtil;
import java.io.Serializable;
/**
* 时长格式化器
* @author Looly
*
* @author Looly
*/
public class BetweenFormater implements Serializable{
public class BetweenFormater implements Serializable {
private static final long serialVersionUID = 1L;
/** 时长毫秒数 */
/**
* 时长毫秒数
*/
private long betweenMs;
/** 格式化级别 */
/**
* 格式化级别
*/
private Level level;
/** 格式化级别的最大个数 */
/**
* 格式化级别的最大个数
*/
private final int levelMaxCount;
/**
* 构造
*
* @param betweenMs 日期间隔
* @param level 级别按照天小时毫秒分为5个等级根据传入等级格式化到相应级别
*/
@ -30,6 +37,7 @@ public class BetweenFormater implements Serializable{
/**
* 构造
*
* @param betweenMs 日期间隔
* @param level 级别按照天小时毫秒分为5个等级根据传入等级格式化到相应级别
* @param levelMaxCount 格式化级别的最大个数假如级别个数为1但是级别到秒那只显示一个级别
@ -45,9 +53,9 @@ public class BetweenFormater implements Serializable{
*
* @return 格式化后的字符串
*/
public String format(){
public String format() {
final StringBuilder sb = new StringBuilder();
if(betweenMs > 0){
if (betweenMs > 0) {
long day = betweenMs / DateUnit.DAY.getMillis();
long hour = betweenMs / DateUnit.HOUR.getMillis() - day * 24;
long minute = betweenMs / DateUnit.MINUTE.getMillis() - day * 24 * 60 - hour * 60;
@ -59,29 +67,29 @@ public class BetweenFormater implements Serializable{
final int level = this.level.ordinal();
int levelCount = 0;
if(isLevelCountValid(levelCount) && 0 != day && level >= Level.DAY.ordinal()){
if (isLevelCountValid(levelCount) && 0 != day && level >= Level.DAY.ordinal()) {
sb.append(day).append(Level.DAY.name);
levelCount++;
}
if(isLevelCountValid(levelCount) && 0 != hour && level >= Level.HOUR.ordinal()){
if (isLevelCountValid(levelCount) && 0 != hour && level >= Level.HOUR.ordinal()) {
sb.append(hour).append(Level.HOUR.name);
levelCount++;
}
if(isLevelCountValid(levelCount) && 0 != minute && level >= Level.MINUTE.ordinal()){
if (isLevelCountValid(levelCount) && 0 != minute && level >= Level.MINUTE.ordinal()) {
sb.append(minute).append(Level.MINUTE.name);
levelCount++;
}
if(isLevelCountValid(levelCount) && 0 != second && level >= Level.SECOND.ordinal()){
if (isLevelCountValid(levelCount) && 0 != second && level >= Level.SECOND.ordinal()) {
sb.append(second).append(Level.SECOND.name);
levelCount++;
}
if(isLevelCountValid(levelCount) && 0 != millisecond && level >= Level.MILLSECOND.ordinal()){
sb.append(millisecond).append(Level.MILLSECOND.name);
if (isLevelCountValid(levelCount) && 0 != millisecond && level >= Level.MILLISECOND.ordinal()) {
sb.append(millisecond).append(Level.MILLISECOND.name);
// levelCount++;
}
}
if(StrUtil.isEmpty(sb)) {
if (StrUtil.isEmpty(sb)) {
sb.append(0).append(this.level.name);
}
@ -90,6 +98,7 @@ public class BetweenFormater implements Serializable{
/**
* 获得 时长毫秒数
*
* @return 时长毫秒数
*/
public long getBetweenMs() {
@ -98,6 +107,7 @@ public class BetweenFormater implements Serializable{
/**
* 设置 时长毫秒数
*
* @param betweenMs 时长毫秒数
*/
public void setBetweenMs(long betweenMs) {
@ -106,6 +116,7 @@ public class BetweenFormater implements Serializable{
/**
* 获得 格式化级别
*
* @return 格式化级别
*/
public Level getLevel() {
@ -114,6 +125,7 @@ public class BetweenFormater implements Serializable{
/**
* 设置格式化级别
*
* @param level 格式化级别
*/
public void setLevel(Level level) {
@ -127,22 +139,41 @@ public class BetweenFormater implements Serializable{
*/
public enum Level {
/** 天 */
/**
*
*/
DAY(""),
/** 小时 */
/**
* 小时
*/
HOUR("小时"),
/** 分钟 */
/**
* 分钟
*/
MINUTE(""),
/** 秒 */
/**
*
*/
SECOND(""),
/** 毫秒 */
MILLSECOND("毫秒");
/**
* 毫秒
* @deprecated 拼写错误请使用{@link #MILLISECOND}
*/
@Deprecated
MILLSECOND("毫秒"),
/**
* 毫秒
*/
MILLISECOND("毫秒");
/** 级别名称 */
/**
* 级别名称
*/
private final String name;
/**
* 构造
*
* @param name 级别名称
*/
Level(String name) {
@ -151,6 +182,7 @@ public class BetweenFormater implements Serializable{
/**
* 获取级别名称
*
* @return 级别名称
*/
public String getName() {
@ -170,7 +202,7 @@ public class BetweenFormater implements Serializable{
* @param levelCount 登记数量
* @return 是否有效
*/
private boolean isLevelCountValid(int levelCount){
private boolean isLevelCountValid(int levelCount) {
return this.levelMaxCount <= 0 || levelCount < this.levelMaxCount;
}
}

View File

@ -165,6 +165,6 @@ public class DateBetween implements Serializable{
@Override
public String toString() {
return toString(BetweenFormater.Level.MILLSECOND);
return toString(BetweenFormater.Level.MILLISECOND);
}
}

View File

@ -525,6 +525,17 @@ public class DateTime extends Date {
*
* @return 毫秒数
*/
public int millisecond() {
return getField(DateField.MILLISECOND);
}
/**
* 获得指定日期的毫秒数部分<br>
*
* @return 毫秒数
* @deprecated 拼写错误请使用{@link #millisecond()}
*/
@Deprecated
public int millsecond() {
return getField(DateField.MILLISECOND);
}

View File

@ -321,9 +321,21 @@ public class DateUtil extends CalendarUtil {
*
* @param date 日期
* @return 毫秒数
* @deprecated 拼写错误请使用{@link #millisecond(Date)}
*/
@Deprecated
public static int millsecond(Date date) {
return DateTime.of(date).millsecond();
return DateTime.of(date).millisecond();
}
/**
* 获得指定日期的毫秒数部分<br>
*
* @param date 日期
* @return 毫秒数
*/
public static int millisecond(Date date) {
return DateTime.of(date).millisecond();
}
/**
@ -426,9 +438,18 @@ public class DateUtil extends CalendarUtil {
/**
* @return 当前日期的毫秒数部分<br>
* @deprecated 拼写错误请使用{@link #thisMillisecond()}
*/
@Deprecated
public static int thisMillsecond() {
return millsecond(date());
return millisecond(date());
}
/**
* @return 当前日期的毫秒数部分<br>
*/
public static int thisMillisecond() {
return millisecond(date());
}
// -------------------------------------------------------------- Part of Date end
@ -1398,7 +1419,7 @@ public class DateUtil extends CalendarUtil {
* @since 3.0.1
*/
public static String formatBetween(long betweenMs) {
return new BetweenFormater(betweenMs, BetweenFormater.Level.MILLSECOND).format();
return new BetweenFormater(betweenMs, BetweenFormater.Level.MILLISECOND).format();
}
/**

View File

@ -10,7 +10,7 @@ public class BetweenFormaterTest {
@Test
public void formatTest(){
long betweenMs = DateUtil.betweenMs(DateUtil.parse("2017-01-01 22:59:59"), DateUtil.parse("2017-01-02 23:59:58"));
BetweenFormater formater = new BetweenFormater(betweenMs, Level.MILLSECOND, 1);
BetweenFormater formater = new BetweenFormater(betweenMs, Level.MILLISECOND, 1);
Assert.assertEquals(formater.toString(), "1天");
}