public static TimeStartAndEndVo parseTime(String startTime, String endTime, String timeType) {
// 1、处理发布时间
TimeStartAndEndVo vo = new TimeStartAndEndVo();
if (StringUtils.isNotEmpty(timeType)) {
switch (timeType) {
case "week":
vo = TimePeriodToNodeUtil.switchToTime(startTime, endTime, -169);
vo.setDayNum(7L);
break;
case "month":
vo = TimePeriodToNodeUtil.switchToTime(startTime, endTime, -721);
vo.setDayNum(30L);
break;
case "threeMonths":
vo = TimePeriodToNodeUtil.switchToTime(startTime, endTime, -2161);
vo.setDayNum(90L);
break;
case "halfYear":
vo = TimePeriodToNodeUtil.switchToTime(startTime, endTime, -4321);
vo.setDayNum(180L);
break;
case "year":
vo = TimePeriodToNodeUtil.switchToTime(startTime, endTime, -8641);
vo.setDayNum(360L);
break;
case "custom":
vo.setStartTime(startTime);
vo.setEndTime(endTime);
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long nd = 1000*24*60*60;//一天的毫秒数
//计算两个时间的毫秒时间差异
long diff = 0L;
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
long day = diff/nd + 1;
vo.setDayNum(day);
break;
default:
break;
}
}
return vo;
}
public static TimeStartAndEndVo switchToTime(String startTime, String endTime, int diff) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.HOUR_OF_DAY,24);
calendar.add(Calendar.SECOND,-1);
endTime = df.format(calendar.getTime());
calendar.set(Calendar.HOUR_OF_DAY,diff);
calendar.add(Calendar.SECOND,1);
calendar.add(Calendar.DATE,1);
startTime = df.format(calendar.getTime());
TimeStartAndEndVo vo = new TimeStartAndEndVo();
vo.setEndTime(endTime);
vo.setStartTime(startTime);
return vo;
}