mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
增加两个日期间隔时间处理接口
This commit is contained in:
parent
fe332dc2dc
commit
cf84d06cce
@ -635,6 +635,57 @@ public class DateUtil extends CalendarUtil {
|
||||
|
||||
return CalendarUtil.formatChineseDate(CalendarUtil.calendar(date), withTime);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回可读的时间间隔,如1天20小时3分48秒
|
||||
* @param milliSecond 毫秒
|
||||
* @author zrh 455741807@qq.com
|
||||
* @return
|
||||
*/
|
||||
public static String formatDuring(long milliSecond) {
|
||||
final int i1000=1000;
|
||||
if (milliSecond <= 0) {
|
||||
return "小于1毫秒";
|
||||
}
|
||||
if (milliSecond < i1000) {
|
||||
return milliSecond + "毫秒";
|
||||
}
|
||||
String result = "";
|
||||
long days = milliSecond / (1000 * 60 * 60 * 24);
|
||||
long hours = (milliSecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
|
||||
long minutes = (milliSecond % (1000 * 60 * 60)) / (1000 * 60);
|
||||
long seconds = (milliSecond % (1000 * 60)) / 1000;
|
||||
if (days > 0) {
|
||||
result += days + "天";
|
||||
}
|
||||
if (hours > 0) {
|
||||
result += hours + "小时";
|
||||
}
|
||||
if (minutes > 0) {
|
||||
result += minutes + "分";
|
||||
}
|
||||
if (seconds >= 0) {
|
||||
result += seconds + "秒";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个日期之间间隔时间
|
||||
* @param begin 开始日期
|
||||
* @param end 结束日期
|
||||
* @author zrh 455741807@qq.com
|
||||
* @return
|
||||
*/
|
||||
public static String formatDuring(Date begin, Date end) {
|
||||
if(begin == null || end == null) {
|
||||
return "";
|
||||
}
|
||||
return formatDuring(end.getTime()-begin.getTime());
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------ Format end ----------------------------------------------
|
||||
|
||||
// ------------------------------------ Parse start ----------------------------------------------
|
||||
|
@ -97,6 +97,29 @@ public class DateUtilTest {
|
||||
Assert.assertEquals(date, parse);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void formatDuring() {
|
||||
long d = 56123456;
|
||||
String s = DateUtil.formatDuring(d);
|
||||
Assert.assertEquals("15小时35分23秒", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatDuring() {
|
||||
Calendar c = Calendar.getInstance();
|
||||
Date s = c.getTime();
|
||||
|
||||
c.add(Calendar.DAY_OF_MONTH, 1);
|
||||
c.add(Calendar.MINUTE, 2);
|
||||
c.add(Calendar.SECOND, 3);
|
||||
Date e = c.getTime();
|
||||
|
||||
String str = DateUtil.formatDuring(s, e);
|
||||
|
||||
Assert.assertEquals("1天2分3秒", str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beginAndEndTest() {
|
||||
String dateStr = "2017-03-01 00:33:23";
|
||||
|
Loading…
Reference in New Issue
Block a user