计算时间差
public static int secondsBetween(Date beforeDate,Date nowDate) throws Exception {
//时间格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
beforeDate=sdf.parse(sdf.format(beforeDate));
nowDate=sdf.parse(sdf.format(nowDate));
Calendar cal = Calendar.getInstance();
cal.setTime(beforeDate);
long time1 = cal.getTimeInMillis();
cal.setTime(nowDate);
long time2 = cal.getTimeInMillis();
return Integer.parseInt(String.valueOf((time2-time1)/(1000)));
}
获取系统前一天
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
return date;
}
获取当前系统时间
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 0);
date = calendar.getTime();
return date;
}
获取当前系统下一天时间
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
return date;
}
判断一个时间是否在一个时间段内
/**
* Description: 判断一个时间是否在一个时间段内 </br>
*
* @param nowTime 当前时间 </br>
* @param beginTime 开始时间 </br>
* @param endTime 结束时间 </br>
*/
private boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(beginTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
return date.after(begin) && date.before(end);
}
}
网友评论