/**
* 前提,开始时间<结束时间
* 天数:计算1.1-1.1 ,1.1-1.2 ,1.3-1.4 一共有2个区间1.1-1.2 ,1.3-1.4 共4 天
* @param list 时间区间列表
*/
public static void distinctDate(List<TestModel> list) {
System.out.println(Arrays.toString(list.toArray()));
//开始时间升序排序,保证数组下标越大,开始时间越大
list.sort((o1, o2) -> o1.getStartDate().compareTo(o2.getEndDate()));
System.out.println(Arrays.toString(list.toArray()));
//时间区间结果,数组下标越大,开始时间越大
List<TestModel> newestModelList = new ArrayList<>();
for (TestModel model : list) {
//时间区间结果为空,直接加入数组
if (newestModelList.size() == 0) {
newestModelList.add(model);
} else {
//最新时间区间
TestModel newestModel = newestModelList.get(newestModelList.size() - 1);
//如果开始时间小于等于最新时间区间的结束时间,
//而且结束时间大于最新时间区间的结束时间,
//将当前结束时间赋值给最新时间区间的结束时间
if (model.getStartDate().getTime() <= newestModel.getEndDate().getTime()) {
if (model.getEndDate().getTime() > newestModel.getEndDate().getTime()) {
newestModel.setEndDate(model.getEndDate());
}
} else {
//如果开始时间大于最新结束时间,加入新的时间区间
newestModelList.add(model);
}
}
}
System.out.println(Arrays.toString(newestModelList.toArray()));
int daySum = 0;
for (TestModel model : newestModelList) {
daySum += (int) ((model.getEndDate().getTime() - model.getStartDate().getTime()) / 86400000) + 1;
}
System.out.println(daySum);
}
实体模型
static class TestModel {
private Timestamp startDate;
private Timestamp endDate;
public TestModel(Timestamp startDate, Timestamp endDate) {
this.startDate = startDate;
this.endDate = endDate;
}
public Timestamp getStartDate() {
return startDate;
}
public void setStartDate(Timestamp startDate) {
this.startDate = startDate;
}
public Timestamp getEndDate() {
return endDate;
}
public void setEndDate(Timestamp endDate) {
this.endDate = endDate;
}
}
字符串转Timestamp
public static Timestamp convertToTimestampByString(String source) {
if (StringUtils.isEmpty(source)) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = sdf.parse(source);
return new Timestamp(date.getTime());
} catch (ParseException e) {
}
return null;
}
调用
public static void main(String[] args) {
Timestamp startDate3 = convertToTimestampByString("2021-12-20");
Timestamp endDate3 = convertToTimestampByString("2021-12-21");
Timestamp startDate2 = convertToTimestampByString("2021-12-21");
Timestamp endDate2 = convertToTimestampByString("2021-12-22");
Timestamp startDate1 = convertToTimestampByString("2021-12-21");
Timestamp endDate1 = convertToTimestampByString("2021-12-23");
TestModel testModel = new TestModel(startDate1, endDate1);
TestModel testMode2 = new TestModel(startDate2, endDate2);
TestModel testMode3 = new TestModel(startDate3, endDate3);
List<TestModel> list = new ArrayList<>();
list.add(testModel);
list.add(testMode2);
list.add(testMode3);
distinctDate(list);
}
结果:
[Test.TestModel(startDate=2021-12-21 00:00:00.0, endDate=2021-12-23 00:00:00.0), Test.TestModel(startDate=2021-12-21 00:00:00.0, endDate=2021-12-22 00:00:00.0), Test.TestModel(startDate=2021-12-20 00:00:00.0, endDate=2021-12-21 00:00:00.0)]
[Test.TestModel(startDate=2021-12-20 00:00:00.0, endDate=2021-12-21 00:00:00.0), Test.TestModel(startDate=2021-12-21 00:00:00.0, endDate=2021-12-22 00:00:00.0), Test.TestModel(startDate=2021-12-21 00:00:00.0, endDate=2021-12-23 00:00:00.0)]
[Test.TestModel(startDate=2021-12-20 00:00:00.0, endDate=2021-12-23 00:00:00.0)]
4