关于日期的比较用calendar的情况。a比c早,返回-1,a=c 返回0 a比c晚 返回1 a.compareTo(c)

SimpleDateFormat onlyTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
Calendar calendarNow = Calendar.getInstance();


java.util.Date reptScheduledTime = onlyTimeFormat.parse(m.get("reptScheduledTime").toString());
java.util.Date start = onlyTimeFormat.parse(m.get("reptStartTime").toString());

java.util.Date end = onlyTimeFormat.parse(m.get("reptEndTime").toString());
calendar.set(Calendar.YEAR,start.getYear());
calendar.set(Calendar.MONTH,start.getMonth());
calendar.set(Calendar.DATE,start.getDate());

calendar.set(Calendar.HOUR_OF_DAY, start.getHours());
calendar.set(Calendar.MINUTE, start.getMinutes());
calendar.set(Calendar.SECOND, start.getSeconds());
System.out.println("calendar"+calendar);

Calendar endCalendar = Calendar.getInstance();
endCalendar.set(Calendar.YEAR,end.getYear());
endCalendar.set(Calendar.MONTH,end.getMonth());
endCalendar.set(Calendar.DATE,end.getDate());

endCalendar.set(Calendar.HOUR_OF_DAY, end.getHours());
endCalendar.set(Calendar.MINUTE, end.getMinutes());
endCalendar.set(Calendar.SECOND, end.getSeconds());
System.out.println("endCalendar:"+endCalendar);
// 如果指定运行时间大于当前时间,则暂时不运行此次任务
//if (calendar.compareTo(calendarNow) >0 &&calendarNow.compareTo(endCalendar)>0)
if (endCalendar.compareTo(calendarNow) > 0) {
paramsUpdateMap.put("pushStatus", "已结束");}
if (calendar.compareTo(calendarNow) > 0||calendarNow.compareTo(endCalendar)>0) {
paramsUpdateMap.put("pushStatus", "未生效");}
else{
paramsUpdateMap.put("pushStatus", "生效中");}
posted @ 2021-08-04 15:27  夏之  阅读(92)  评论(0编辑  收藏  举报