group by 分组出来统计的时候,不能补全,所以用java代码把没有时间的数据补全
public class Test {
@org.junit.Test
public void m1() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 6, 0, 0, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> strings = new ArrayList<>();
strings.add("2019-08-12");
strings.add("2019-08-14");
//存在list的哪个位置
int flag = 0;
//为0 近七天
for (int i = 0; i < 6; i++) {
if (!strings.contains(sdf.format(calendar.getTime()))){
strings.add(flag,sdf.format(calendar.getTime()));
}
flag ++;
calendar.add(Calendar.DAY_OF_MONTH, 1);
// System.out.println(sdf.format(calendar.getTime()));
}
//
for (int i = 0; i < strings.size(); i++) {
System.out.println(strings.get(i));
}
}
/**
获取近7天的零点
*/
public static Long getWeekZero() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 6, 0, 0, 0);
Long zero = calendar.getTime().getTime();
return zero;
}
}
补全近72小时的时间
@org.junit.Test
public void m2() {
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH");
List<String> strings = new ArrayList<>();
strings.add("2019-08-13 22");
strings.add("2019-08-13 23");
Calendar calendar1 = Calendar.getInstance();
calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH) , calendar1.get(Calendar.HOUR_OF_DAY)-72, 0, 0);
int flag = 0;//设置在哪里
//为1 小时
for (int i = 0; i < 72; i++) {
if (!strings.contains(sdf2.format(calendar1.getTime()))){
strings.add(flag,sdf2.format(calendar1.getTime()));
}
flag ++;
calendar1.add(Calendar.HOUR_OF_DAY, 1);
// System.out.println(sdf2.format(calendar1.getTime()));
}
for (int i = 0; i < strings.size(); i++) {
System.out.println(strings.get(i));
}
}