int times = 0;//初始值
if ((StringUtils.isNotEmpty(bgt)) && (StringUtils.isNotEmpty(edt))) {//获取初始时间和结束时间
Calendar cal = Calendar.getInstance();//定义一个calendar cal
SimpleDateFormat sdfd = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");//定义时间格式
Date beginTm = sdfd.parse(bgt);//将string类型的开始时间转换为date类型
Date endTm = sdfd.parse(edt);//将string类型的结束时间转换为date类型
for (int i = 0; i <= (endTm.getTime() - beginTm.getTime()) / 1000; i++) {//结束时间减去开始时间/1000获得中间时间的秒数
Date afterDate = new Date(beginTm.getTime() + 1000 * i);//循环遍历每一次循环+1秒
cal.setTime(afterDate);//set进日历 进入判断
if (cal.get(Calendar.MONTH) + 1 >= 6 && cal.get(Calendar.MONTH) + 1 < 10) {//如果是6-10月份那么就是汛期
if (cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0) {//如果分钟和秒都为0 那么times+1 做一次判断
times += 1;//返回值每一小时+1
}
} else {
if (cal.get(Calendar.HOUR_OF_DAY) == 8 && cal.get(Calendar.MINUTE) == 0 && cal.get(Calendar.SECOND) == 0) {//判断非汛期时间 如果时间为8点 分钟为8:00 那么今天返回值+1
times += 1;
}
}
}
}
return times;//返回times 确认汛期回报次数
方法介绍
应上报量计算,汛期(6.1-10.1),一个小时上报一次,非汛期一天上报一次
给个时间段,计算这段时间应该上报多少次