add method

This commit is contained in:
Looly
2020-09-22 09:35:07 +08:00
parent d6784ca99e
commit 46fafeeb86
6 changed files with 48 additions and 47 deletions

View File

@@ -1383,30 +1383,6 @@ public class DateUtil extends CalendarUtil {
return new DateBetween(beginDate, endDate).betweenMonth(isReset);
}
/**
* 获取两个日期之间所有的月份
* @param start 开始时间
* @param end 结束时间
* @return List<String> 格式为yyyMM格式的月份列表 包含收尾</>
* @since 5.4.4
*/
public static List<String> getBetweenMonths(Date start, Date end) {
List<String> result = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
// 加一个月,保证开始和结束同步时返回当月
tempStart.add(Calendar.MONTH, 1);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
result.add(sdf.format(start));
while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
result.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.MONTH, 1);
}
return result;
}
/**
* 计算两个日期相差年数<br>
* 在非重置情况下如果起始日期的月小于结束日期的月年数要少算1不足1年

View File

@@ -42,7 +42,8 @@ import java.util.TreeSet;
public class NetUtil {
public final static String LOCAL_IP = "127.0.0.1";
public static String LOCAL_HOSTNAME = "";
public static String localhostName;
/**
* 默认最小端口1024
@@ -535,20 +536,26 @@ public class NetUtil {
}
/**
* 获取主机名称
* 获取主机名称,一次获取会缓存名称
*
* @return 主机名称
* @since 5.4.4
*/
public static String getLocalHostName() {
try {
if (StrUtil.isNotBlank(LOCAL_HOSTNAME)) {
return LOCAL_HOSTNAME;
}
LOCAL_HOSTNAME = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
LOCAL_HOSTNAME = getLocalhostStr();
if (StrUtil.isNotBlank(localhostName)) {
return localhostName;
}
return LOCAL_HOSTNAME;
final InetAddress localhost = getLocalhost();
if(null != localhost){
String name = localhost.getHostName();
if(StrUtil.isEmpty(name)){
name = localhost.getHostAddress();
}
localhostName = name;
}
return localhostName;
}
/**

View File

@@ -3,7 +3,6 @@ package cn.hutool.core.date;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.BetweenFormater.Level;
import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@@ -820,12 +819,4 @@ public class DateUtilTest {
final DateTime parse = DateUtil.parse(dt);
Assert.assertEquals("2020-06-03 12:32:12", parse.toString());
}
@Test
public void getBetweenMonthsTest() {
List<String> months1 = DateUtil.getBetweenMonths(new Date(), new Date());
Assert.assertEquals(1, months1.size());
List<String> months = DateUtil.getBetweenMonths(DateUtil.parse("2020-05-08 3:12:3"), new Date());
Assert.assertEquals(5, months.size());
}
}