1. package com.zym.testpoi.util;
    2. import java.util.Calendar;
    3. import java.util.Date;
    4. public class DateUtil {
    5. public static int differentDays(Date date1, Date date2){
    6. Calendar calendar1 = Calendar.getInstance();
    7. calendar1.setTime(date1);
    8. Calendar calendar2 = Calendar.getInstance();
    9. calendar2.setTime(date2);
    10. int day1 = calendar1.get(Calendar.DAY_OF_YEAR);
    11. int day2 = calendar2.get(Calendar.DAY_OF_YEAR);
    12. int year1 = calendar1.get(Calendar.YEAR);
    13. int year2 = calendar2.get(Calendar.YEAR);
    14. if (year1 != year2){
    15. int timeDistance = 0;
    16. for (int i = year1;i<year2;i++){
    17. if (i%4 == 0 && i%100!=0 || i%400 == 0){ //闰年
    18. timeDistance += 366;
    19. }else{
    20. timeDistance += 365;
    21. }
    22. }
    23. return timeDistance + (day2 - day1);
    24. }else {
    25. return day2 - day1;
    26. }
    27. }
    28. }

    测试类:

    1. @Test
    2. public void checkDifferentDays() throws ParseException {
    3. String paramDate = "2020-09-20 10:00:00";
    4. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    5. Date date = simpleDateFormat.parse(paramDate);
    6. int i = DateUtil.differentDays(new Date(),date);
    7. System.out.println(i);
    8. }