根据输入日期,按小时 统计距离当前时间 的所有数据。

controller

/**
   * 统计本周异常数据
   * @param strDate
   * @return
   */
  @GetMapping("/week")
  public ResponseEntity<Message> statisDeviceStatusByWeek(String strDate) {
    Message message = new Message();
    try {
      if(null == strDate){
        strDate = sdf1.format(getWeekBegin());
      }
      Map<Long, Map<String, Object>> deviceStatusMap = service.statusDeviceStatusByDate(strDate);
      message.setData(deviceStatusMap);
    } catch (Exception e){
      message.setCode(100001);
      message.setMessage("week出错");
    }
    return new ResponseEntity<>(message, HttpStatus.OK);
  }
  /**
   * 统计今天异常
   * @param strDate
   * @return
   */
  @GetMapping("/today")
  public ResponseEntity<Message> statisDeviceStatusByDay(String strDate){
    Message message = new Message();
    try {
      if(null == strDate){
        strDate = sdf1.format(new Date());
      }
      Map<Long, Map<String, Object>> deviceStatusMap = service.statusDeviceStatusByDate(strDate);
      message.setData(deviceStatusMap);
    } catch (Exception e){
      message.setCode(100001);
      message.setMessage("today出错");
    }
    return new ResponseEntity<>(message, HttpStatus.OK);
  }
 /**
   * 获取本周一日期
   */
  private String getWeekBegin(){
    Calendar cal = Calendar.getInstance();
    //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    //获得当前日期是一个星期的第几天
    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
    if(dayWeek==1){
      dayWeek = 8;
    }
    // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
    cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
    Date mondayDate = cal.getTime();
    String weekBegin = sdf1.format(mondayDate);
//    System.out.println("所在周星期一的日期:" + weekBegin);
    return weekBegin;
  }
}

service

 public Map<Long, Map<String, Object>> statusDeviceStatusByDate(String strDate) {
    String nowTime1 = sdf1.format(new Date());
    Long nowTime = Long.valueOf(dateTimeStamp(nowTime1, "yyyy-MM-dd HH:mm:ss"));
    Long startTime = Long.valueOf(dateTimeStamp(strDate, "yyyy-MM-dd HH:mm:ss"));
    Map<Long, Map<String, Object>> deviceStatusMap = new HashMap();
    while (startTime <= nowTime) {
      Map<String, Object> map = new HashMap<>();
      Long beforTime = startTime;
//      Date afterHourTime = dateRoll(new Date(beforTime),Calendar.HOUR,-1);
//      Long afterTime = Long.valueOf(dateTimeStamp(sdf2.format(afterHourTime),"yyyy-MM-dd HH:mm:ss"));
      Long afterTime = beforTime + 3600;
      String a = timeStampDate(String.valueOf(beforTime), "yyyy-MM-dd HH:mm:ss");
      String b = timeStampDate(String.valueOf(afterTime), "yyyy-MM-dd HH:mm:ss");
      int normal = tagDeviceAllRepository.findDeviceAllRecordeByHour(0, a, b);
      int exception = tagDeviceAllRepository.findDeviceAllRecordeByHour(1, a, b);
      int warning = tagDeviceAllRepository.findDeviceAllRecordeByHour(2, a, b);
      map.put("normal", normal);
      map.put("exception", exception);
      map.put("warning", warning);
      deviceStatusMap.put(afterTime, map);
//      map.clear();
      startTime = afterTime;
    }
    return deviceStatusMap;
  }
  /**
   * 日期格式字符串转换成时间戳
   *
   * @param date_str 字符串日期
   * @param format 如:yyyy-MM-dd HH:mm:ss
   */
  public String dateTimeStamp(String date_str, String format) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      return String.valueOf(sdf.parse(date_str).getTime() / 1000);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }
  /**
   * 时间戳转换成日期格式字符串
   *
   * @param seconds 精确到秒的字符串
   * @param format 如:yyyy-MM-dd HH:mm:ss
   */
  public String timeStampDate(String seconds, String format) {
    if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
      return "";
    }
    if (format == null || format.isEmpty()) {
      format = "yyyy-MM-dd HH:mm:ss";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(new Date(Long.valueOf(seconds + "000")));
  }

不用

  /**
   * 实现当前时间往前推N小时
   *
   * @param date 精确到秒的字符串 new Date()
   * @param i Calendar.HOUR
   * @param d 1
   */
  public Date dateRoll(Date date, int i, int d) {
    // 获取Calendar对象并以传进来的时间为准
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    // 将现在的时间滚动固定时长,转换为Date类型赋值
    calendar.add(i, d);
    // 转换为Date类型再赋值
    date = calendar.getTime();
    return date;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值