统计任意时间段、近一小时、近一天、近一周、近一月

-做好类型判断

 private List<String> selectDate(String type, String startDate, String endDate) {
        if ("0".equals(type)) {
            //1小时
            return DateUtils.oneHourList();
        } else if ("1".equals(type)) {
            //1天
            return DateUtils.oneDayList();
        } else if ("2".equals(type)) {
            //七天
            return DateUtils.sevenDayList();
        } else if ("3".equals(type)) {
            //一个月
            return DateUtils.thirtyDayList();
        } else if ("4".equals(type)) {
            //指定时间段
            return DateUtils.twoDatesList(startDate, endDate);
        }
        return null;
    }

-每个类型时间存入list

	//获取当前时间的一个小时
	public static List<String> oneHourList() {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		// 当前时间前60分钟
		calendar.add(Calendar.MINUTE, -59);
		Date startDate = calendar.getTime();
		calendar.setTime(new Date());
		Date endDate = calendar.getTime();
		List<String> XDate = new ArrayList<String>();
		boolean flag = true;
		while (flag) {
			if (sdf1.format(startDate).equals(sdf1.format(endDate))) {
				XDate.add(sdf1.format(startDate));
				flag = false;
			} else {
				XDate.add(sdf1.format(startDate));
				Calendar cal = Calendar.getInstance();
				cal.setTime(startDate);
				cal.add(Calendar.MINUTE, 1);
				startDate = cal.getTime();
			}
		}
		return XDate;
	}

	//获取近一天的数据
	public static List<String> oneDayList() {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		// 当前时间前11小时
		calendar.add(Calendar.HOUR, -23);
		Date startDate = calendar.getTime();
		calendar.setTime(new Date());
		Date endDate = calendar.getTime();
		List<String> XDate = new ArrayList<String>();
		boolean flag = true;
		while (flag) {
			if (sdf1.format(startDate).equals(sdf1.format(endDate))) {
				XDate.add(sdf1.format(startDate));
				flag = false;
			} else {
				XDate.add(sdf1.format(startDate));
				Calendar cal = Calendar.getInstance();
				cal.setTime(startDate);
				cal.add(Calendar.HOUR_OF_DAY, 1);
				startDate = cal.getTime();
			}
		}
		return XDate;
	}

	//获取近七天的数据
	public static List<String> sevenDayList() {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		//获取一个星期前的时间
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -6);
		Date startDate = calendar.getTime();
		//获取当前时间时间
		calendar.setTime(new Date());
		Date endDate = calendar.getTime();
		List<String> XDate = new ArrayList<String>();
		boolean flag = true;
		while (flag) {
			if (sdf1.format(startDate).equals(sdf1.format(endDate))) {
				XDate.add(sdf1.format(startDate));
				flag = false;
			} else {
				XDate.add(sdf1.format(startDate));
				Calendar cal = Calendar.getInstance();
				cal.setTime(startDate);
				cal.add(Calendar.DATE, 1);
				startDate = cal.getTime();
			}
		}
		return XDate;
	}

	//近三十天
	public static List<String> thirtyDayList() {
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		//获取一个月前的时间
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -29);
		Date startDate = calendar.getTime();
		//获取当前时间时间
		calendar.setTime(new Date());
		Date endDate = calendar.getTime();
		List<String> XDate = new ArrayList<String>();
		boolean flag = true;
		while (flag) {
			if (sdf1.format(startDate).equals(sdf1.format(endDate))) {
				XDate.add(sdf1.format(startDate));
				flag = false;
			} else {
				XDate.add(sdf1.format(startDate));
				Calendar cal = Calendar.getInstance();
				cal.setTime(startDate);
				cal.add(Calendar.DATE, 1);
				startDate = cal.getTime();
			}
		}
		return XDate;
	}

	/**
	 * 获取两个时间段内的日期
	 *
	 * @param sDate
	 * @param eDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> twoDatesList(String sDate, String eDate) {
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = sdf1.parse(sDate);
			Date endDate = sdf1.parse(eDate);
			List<String> XDate = new ArrayList<String>();
			boolean flag = true;
			while (flag) {
				if (sdf1.format(startDate).equals(sdf1.format(endDate))) {
					XDate.add(sdf1.format(startDate));
					flag = false;
				} else {
					XDate.add(sdf1.format(startDate));
					Calendar cal = Calendar.getInstance();
					cal.setTime(startDate);
					cal.add(Calendar.DATE, 1);
					startDate = cal.getTime();
				}
			}
			return XDate;
		} catch (ParseException e) {
				
		}
		return null;
	}

-把需要的时间填入list,当作主表左关联你的数据表

 SELECT
        a.date AS `date`,
        cpuUsagePercent AS rate
        FROM
        (
        <foreach collection="list" index="index" item="item" open="(" separator="UNION ALL" close=")">
            select #{item} date
        </foreach>
        ) a LEFT JOIN (
        SELECT
        <choose>
            <when test="alarmAnalyseDTO.type == 4">
                DATE_FORMAT( olh.createTime,'%Y-%m-%d')
            </when>
            <when test="alarmAnalyseDTO.type == 3">
                DATE_FORMAT( olh.createTime,'%Y-%m-%d')
            </when>
            <when test="alarmAnalyseDTO.type == 2">
                DATE_FORMAT( olh.createTime,'%Y-%m-%d')
            </when>
            <when test="alarmAnalyseDTO.type == 1">
                DATE_FORMAT( olh.createTime,'%Y-%m-%d %H')
            </when>
            <when test="alarmAnalyseDTO.type == 0">
                DATE_FORMAT( olh.createTime,'%Y-%m-%d %H:%i')
            </when>
        </choose>
        `date`,
        cpuUsagePercent,
        olh.createTime
        FROM
        ods_ly_hostkylincpus AS olh
        LEFT JOIN ods_ly_alertlist AS ola ON olh.jobId = ola.jobId
        WHERE
        ola.idField = #{alarmAnalyseDTO.alarmId}
        group by `date`
        ) b on a.date = b.date
        order by `date`

-hutool有封装的方法可以根据业务场景具体使用

// 两个时间的区间 返回[2017-01-01 00:00:00, 2017-01-02 00:00:00, 2017-01-03 00:00:00] 
 List<DateTime> rangeToList = DateUtil.rangeToList(DateUtil.parse("00:00:00"),
                DateUtil.parse("24:00:00"), DateField.HOUR_OF_DAY);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值