计算同一天内,两个时间之间的分钟数:
/**
* <pre>
* 计算同一天内,两个时间之间的分钟数
* </pre>
*
* @param date1 时间1
* @param date2 时间2
* @return betweenMinutes 两个时间的间隔分钟数
*/
public static int getBetweenMinutes(Date date1, Date date2) {
if (date1 == null || date2 == null) {
return -1;
}
int betweenMinutes; //两个时间间隔分钟数
int betweenHours; //两个时间间隔小时数
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
if (c1.after(c2)) { //确保第二个时间一定大于第一个时间
c2.setTime(date1);
c1.setTime(date2);
}
betweenHours = c2.get(Calendar.HOUR_OF_DAY)
- c1.get(Calendar.HOUR_OF_DAY);
betweenMinutes = c2.get(Calendar.MINUTE) - c1.get(Calendar.MINUTE);
betweenMinutes += betweenHours * 60;
return betweenMinutes;
}