填充处理 代码:
/**填充处理*/
String start = vehicleRecordInfo.getStartTime();
String end = vehicleRecordInfo.getEndTime();
try {
Date date1 = sdf.parse(start);
Date date2 = sdf.parse(end);
int daySub = differentDays(date1,date2); //间隔天数
List<String> dateList = genDateRange(date1,date2); //存放连续日期的list
if(daySub==0){ //间隔天数为0,同一天
if(list.size()==0){
VehicleRecordHour vrh = vrhFilling(); //调用填充数据方法vrhFilling
Date dtList = sdf.parse(dateList.get(0));
vrh.setStaticsDate(dtList); //单独set日期
list.add(vrh);
}
}else {
final int listSize = list.size(); //填充会改变size,设置为final
for (int i = 0; i <= daySub; i++) {
//如果list为空,直接填充
if (listSize == 0) {
VehicleRecordHour vrh = vrhFilling(); //调用填充数据方法vrhFilling
Date dtList = sdf.parse(dateList.get(i));
vrh.setStaticsDate(dtList); //单独set日期
list.add(vrh);
}
//list不为空,就需要对比
for (int j = 0; j < listSize; j++) {
Boolean boo;
Date sli = list.get(j).getStaticsDate();
String reStr = sdf.format(sli);
if (!dateList.get(i).equals(reStr)) { //如果数据库没有该日期dateList.get(i)
boo = false; //没有
} else {
break;
}
if (j == listSize - 1 && boo == false) { //到最后都没有该日期
VehicleRecordHour vrh = vrhFilling(); //调用填充数据方法vrhFilling
Date dtList = sdf.parse(dateList.get(i));
vrh.setStaticsDate(dtList); //单独set日期
list.add(vrh);
}
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
list.sort(Comparator.comparing(VehicleRecordHour::getStaticsDate));
PageInfo page = new PageInfo(list);
return RestResult.success(new PageEntity(page));
附:
1、指定日期范围获取日期列表
/**
* 生成日期区间
* @param start1 e.g. 2018-08-28 12:00:00
* @param end1 e.g. 2018-08-30 10:10:00
* @return [2018-08-28, 2018-08-29, 2018-08-30]
*/
public static List<String> genDateRange(Date start1, Date end1) {
DateTime start=new DateTime(start1);
DateTime end=new DateTime(end1);
List<String> list = new ArrayList<>();
if (start.isAfter(end)) {
return list;
}
DateTime _start = new DateTime(start.getYear(), start.getMonthOfYear(), start.getDayOfMonth(), 0, 0, 0);
DateTime _end = new DateTime(end.getYear(), end.getMonthOfYear(), end.getDayOfMonth(), 0, 0, 0);
DateTime date = new DateTime(_start);
while (true) {
if (!date.isAfter(_end)) {
list.add(date.toString(CommonUtils.DATE));
date = date.plusDays(1);
} else {
break;
}
}
return list;
}
2、日期处理,将传来的日期自定义拆分处理
/**
*日期处理,joda-time
*/
Record record = new Record();
//日期拆分,传入的实体为recordEntity
if(recordEntity.getRecordTime()!=null) {
Long recordTime = recordEntity.getRecordTime();
Date recordDateTime = new Date(recordTime);
//Date转成Calendar
Calendar cal = Calendar.getInstance();
cal.setTime(recordDateTime);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1; //获取的月份0开始的
int date = cal.get(Calendar.DATE);
int hour = cal.get(Calendar.HOUR);
//joda-time
Date staticsDate = new DateTime(year, month, date, 0, 0, 0).toDate();
Date staticsMonth = new DateTime(year, month, 1, 0, 0, 0).toDate(); //值为 Wed Aug 01 00:00:00 CST 2018
record.setStaticsDate(staticsDate);
record.setStaticsDate(staticsMonth);
record.setSnapYear(year);
record.setSnapMonth(month);
record.setSnapDate(date);
record.setSnapHour(hour);
record.setSnapTime(recordDateTime);
}
3、两日期间隔天数
/**
* 两个时间的间隔天数 joda-time
* @param date1
* @param date2
* @return
*/
public static int differentDays(Date date1,Date date2)
{
LocalDate start=new LocalDate(date1);
LocalDate end=new LocalDate(date2);
int days = Days.daysBetween(start, end).getDays();
return days;
}