计算两个日期相差几年几月几天,考虑闰年平年

java8以下 计算两个日期相差几年几月几天,考虑闰年平年

  //    java 计算两个日期相差几年几月几天,考虑闰年平年
    public void calculateDifference(String startDade, String endDate) {

        Calendar calendar1 = Calendar.getInstance(); // 第一个日期,默认当前日期
        Calendar calendar2 = Calendar.getInstance(); // 第二个日期,默认当前日期

        // 设置第一个日期的年、月、日
        calendar1.set(Calendar.YEAR, RexgUtil.getYMD(startDade)[0]);
        calendar1.set(Calendar.MONTH, RexgUtil.getYMD(startDade)[1]);
        calendar1.set(Calendar.DAY_OF_MONTH, RexgUtil.getYMD(startDade)[2]);

        if (null != endDate) {
            calendar2.set(Calendar.YEAR, RexgUtil.getYMD(endDate)[0]);
            calendar2.set(Calendar.MONTH, RexgUtil.getYMD(endDate)[1]);
            calendar2.set(Calendar.DAY_OF_MONTH, RexgUtil.getYMD(endDate)[2]);
        }
        int yearDifference = calendar2.get(Calendar.YEAR) - calendar1.get(Calendar.YEAR);
        int monthDifference = calendar2.get(Calendar.MONTH) - calendar1.get(Calendar.MONTH);
        int dayDifference = calendar2.get(Calendar.DAY_OF_MONTH) - calendar1.get(Calendar.DAY_OF_MONTH);

        // 考虑日期超出状况,需要从年或者月借位
        if (dayDifference < 0) {
            monthDifference -= 1;
            int month = calendar1.get(Calendar.MONTH);
            int year = calendar1.get(Calendar.YEAR);

            // 判断是否为2月,考虑闰年平年
            if (month == Calendar.FEBRUARY) {
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    dayDifference += 29;
                } else {
                    dayDifference += 28;
                }
            } else if (month == Calendar.APRIL ||
                    month == Calendar.JUNE ||
                    month == Calendar.SEPTEMBER ||
                    month == Calendar.NOVEMBER) {
                dayDifference += 30;
            } else {
                dayDifference += 31;
            }
        }

        if (monthDifference < 0) {
            yearDifference -= 1;
            monthDifference += 12;
        }

        System.out.println("Years: " + yearDifference);
        System.out.println("Months: " + monthDifference);
        System.out.println("Days: " + dayDifference);

    }



public class RexgUtil {

    public static int[] getYMD(String yyyymmddStr) {
        int[] ymd= new int[3];
//        String input = "2023年08月03日";

        // 使用正则表达式匹配年、月、日
        String pattern = "([0-9]{4})年([0-9]{2})月([0-9]{2})日";
        Pattern regex = Pattern.compile(pattern);
        Matcher matcher = regex.matcher(yyyymmddStr);

        if (matcher.find()) {
            String year = matcher.group(1);
            String month = matcher.group(2);
            String day = matcher.group(3);

//            System.out.println("年: " + year);
//            System.out.println("月: " + month);
//            System.out.println("日: " + day);

            ymd[0] = Integer.parseInt(year);
            ymd[1] = Integer.parseInt(month);
            ymd[2] = Integer.parseInt(day);
        }
        return ymd;
    }
}

如:2015年1月9日至今相差

计算两个日期相差几年几月几天,考虑闰年平年_第1张图片

 

jdk8以上的代码更简洁:

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        //设置开始日期和结束日期
        LocalDate startDate = LocalDate.of(2010, 1, 1);
        LocalDate endDate = LocalDate.of(2020, 9, 20);

        //按整月计算差值
        long monthsBetween = ChronoUnit.MONTHS.between(startDate, endDate);
        long years = monthsBetween / 12;
        long remainMonths = monthsBetween % 12;

        //按天计算差值
        long daysBetween = ChronoUnit.DAYS.between(startDate.plusMonths(monthsBetween), endDate);
        
        System.out.println("相差" + years + "年" + remainMonths + "月" + daysBetween + "天");
        
      //或者你也可以使用Period做更精确的计算
        Period period = Period.between(startDate, endDate);
        System.out.println("相差" + period.getYears() + "年" + period.getMonths() + "月" + period.getDays() + "天");
    }
}

你可能感兴趣的:(java,时间差)