计算两个日期之间的年数
public static int yearsBetween(String firstDate, String secondDate) {
Date std1 = strToDate(firstDate);
Date std2 = strToDate(secondDate);
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(std1);
cal2.setTime(std2);
int firstYear = cal1.get(Calendar.YEAR);
int secondYear = cal2.get(Calendar.YEAR);
return (secondYear - firstYear);
}
计算两个日期之间的天数
public static int daysBetween(String currentDate, String receiveDate) {
SimpleDateFormat sdf = new SimpleDateFormat(yMd);
Date currentDay = null, receiveDay = null;
try {
currentDay = sdf.parse(currentDate);
receiveDay = sdf.parse(receiveDate);
} catch (ParseException e) {
logger.warn("日期转换异常!!!"+e.getMessage());
}
Calendar cal = Calendar.getInstance();
cal.setTime(currentDay);
long time1 = cal.getTimeInMillis();
cal.setTime(receiveDay);
long time2 = cal.getTimeInMillis();
long days = (time2 - time1) / (1000 * 3600 * 24) + 1;
return Integer.parseInt(String.valueOf(days));
}