判断是否超过24小时(一天)的方法1

如果小于等于24小时为true;大于24小时为false。参数是更新时间

public boolean isIn24H(Date updateTime){
Date date = new Date();
String updateTime1 = updateTime.toString().substring(0,19);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date end = null;
try {
String format = sdf.format(date);
end = sdf.parse(format);
java.util.Date start = sdf.parse(updateTime1);
long cha = end.getTime() - start.getTime();
double result = cha * 1.0 / (1000 * 60 * 60);
if(result<=24){
return true;
}else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}


参数是一个List集合(与上面的方法一块使用)
public Boolean onLine(List<MonitorDevice> findDrainage){
List<MonitorDevice> collect = findDrainage.stream().filter(s -> isIn24H(s.getUpdateTime())).collect(Collectors.toList());
if (collect.size()>0){
return true;
}else {
return false;
}
}
eg:
@GetMapping("/queryElevatorStatus")
public Result queryElevatorStatus(String regionName,String deviceType,String deviceName) {
try {

String stationId = this.getStation().getId();
//新版加入离线在线
List<FtqDTO> list = environmentMonitoringService.queryElevatorStatus(stationId,regionName,deviceType,deviceName);

//判断直梯扶梯设备是否在线
DeviceOnLine deviceOnLine = new DeviceOnLine();
List<FtqDTO> collect = list.stream().filter(s-> {
boolean isNotNull= false;
if(s.getModifyTime() != null) {
isNotNull = deviceOnLine.isIn24H(s.getModifyTime());
}
return isNotNull;
}).collect(Collectors.toList());

if (collect.size()==0) {
List<FtqDTO> collect1 = list.stream().map(s -> {
s.setFaultStatus("-1"); // 服务离线
return s;
}).collect(Collectors.toList());
return Result.ok(collect1);
}
return Result.ok(list);
} catch (Exception e) {
e.printStackTrace();
return Result.error(null);
}
}

posted @ 2022-10-25 14:10  sensen~||^_^|||&  阅读(806)  评论(0)    收藏  举报