今天有个需求是要做统计两个时间段的数据,比如:
2021-10-12 13:00~2021-10-13 23:59:59秒正常传入数据库直接就查询到了数据,但是echar需要时间轴而且我的数据是一秒一帧的数据 所以需要我统计数据的过程也需要把时间过程传给前端展示y轴,上网查了都没有这种方式,所以自己写了一波:
public static List getTimeList(int timeScale,Date startTime,Date endTime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
List result = new ArrayList();
while (startTime.compareTo(endTime) < 0) {
result.add(sdf.format(startTime));
//日期
startTime = addFiveMin(startTime, timeScale);
}
return result;
}
private static Date addFiveMin(Date start, int offset) {
Calendar c = Calendar.getInstance();
c.setTime(start);
c.add(Calendar.MINUTE, offset);
return c.getTime();
}
main函数:
public static void main(String[] args) {
String beginDate="2021-10-10 15:00:00";
String endDate="2021-10-12 15:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
List ds = getTimeList(1,sdf.parse(beginDate),sdf.parse(endDate));
System.out.println(ds);
}catch (Exception e){
e.printStackTrace();
}
}
运行结果:



