/**
* 判断两个日期间是否超过的年数
* @param time1
* @param time2
* @param numYear
* @return
*/
public static Boolean DateCompare(Date time1,Date time2,int numYear) {
Date time3 = add(time1, Calendar.YEAR,numYear);
if(time3.getTime()<time2.getTime()){
return true;
}
return false;
}
/**
* 时间加减
* @param date
* @param calendarField :Calendar.YEAR/ Calendar.MONTH /Calendar.DAY
* @param amount
* @return
*/
public static Date add(final Date date, final int calendarField, final int amount) {
if (date == null) {
return null;
}
final Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
测试一下:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
/**
* 超过一年?
*/
System.out.println("-----"+DateCompare(sdf.parse("2020-01-31 22:27:35"),sdf.parse("2022-05-06 22:27:35"),1));
/**
* 超过三年?
*/
System.out.println("-----"+DateCompare(sdf.parse("2020-01-31 22:27:35"),sdf.parse("2022-05-06 22:27:35"),3));
}
结果如下: