package com.liang.common.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author ly
 * @Date 2024/9/23 17:42
 * @Version 1.0
 */
public class time {
    // 计算年差异
    public static int calculateYearDifference(Date startDate, Date endDate) {
        // 使用 Calendar 类来获取年份信息
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);

        int startYear = startCalendar.get(Calendar.YEAR);
        int endYear = endCalendar.get(Calendar.YEAR);

        // 计算年份差异
        return endYear - startYear;
    }
    /**
     * 计算天数差异
     */
    public static long calculateDaysDifference(Date startDate, Date endDate) {
        // 获取时间戳（毫秒）
        long startMillis = startDate.getTime();
        long endMillis = endDate.getTime();

        // 计算天数差异
        long diffMillis = endMillis - startMillis;
        return diffMillis / (1000 * 60 * 60 * 24); // 将毫秒转换为天
    }
}
