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

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)+(null != endDate ? 0 : 1) - 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 (yearDifference <= 0) {
            // 考虑闰年的情况
            if (monthDifference < 0 || (monthDifference == 0 && dayDifference < 0)) {
                yearDifference--;
                monthDifference += 12;
            }

            dayDifference = monthDifference <=0 ? (dayDifference+1) : dayDifference;
        }


        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日至今相差

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() + "天");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值