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;
}