1、计算两个Date日期相差多少天、时、分、秒、毫秒等
public static int getDiffDays(Date start, Date end) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long diff = end.getTime() - start.getTime();
long day = diff / nd;
long hour = diff % nd / nh;
long min = diff % nd % nh / nm;
long sec = diff % nd % nh % nm / ns;
return Integer.valueOf(String.valueOf(day));
}
2、日期Date往后推或往前移指定天数
public static Date getNewDate(Date startDate, int i) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate);
calendar.add(calendar.DATE, i);
return calendar.getTime();
}
3、判断Date是否是周六或周天
public static boolean isWeekend(Date newDate) {
boolean result = false;
Calendar calendar = Calendar.getInstance();
calendar.setTime(newDate);
int week_index = calendar.get(Calendar.DAY_OF_WEEK);
if(week_index==1 || week_index == 7){
return true;
}
return result;
}