获取两个时间年月日时间差

/**
     * 获取两个时间年月日时间差
     *
     * @param startDate    开始时间
     * @param endDate       结束时间
     * @return java.lang.String
     * @date 2020/7/8 17:09
     */
    public static String calcTimeDiff(String startDate,String endDate) {
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        StringBuilder sb = new StringBuilder();
        Calendar startTime  =  Calendar.getInstance();
        Calendar  endTime =  Calendar.getInstance();
        try {
            startTime.setTime(sdf.parse(startDate));
            endTime.setTime(sdf.parse(endDate));
        } catch (ParseException e) {
            e.printStackTrace();
           return "-";
        }

        if(endTime.getTimeInMillis() <= startTime.getTimeInMillis()){
            return "-";
        }
        int toYear = startTime.get(Calendar.YEAR);
        int toMonth = startTime.get(Calendar.MONTH) + 1;
        int toDay = startTime.get(Calendar.DAY_OF_MONTH);
        int fromYear = endTime.get(Calendar.YEAR);
        int fromMonth = endTime.get(Calendar.MONTH) + 1;
        int fromDay = endTime.get(Calendar.DAY_OF_MONTH);

        int fromTotalMonth = fromYear * 12 + fromMonth;
        int toTotalMonth = toYear * 12 + toMonth;

        int diffMonth = fromTotalMonth - toTotalMonth;

        if(diffMonth >= 12){
            sb.append(diffMonth/12);
            sb.append("年");
        }
        int modMonth = diffMonth%12;
        if(diffMonth > 0 && modMonth > 0){
            startTime.add(Calendar.MONTH, diffMonth);
            boolean checkMin = startTime.getTimeInMillis() < endTime.getTimeInMillis();

            if(checkMin || (toDay == fromDay)){
                sb.append(modMonth);
                sb.append("月");
            }else{
                sb.append(modMonth -1);
                sb.append("月");
            }
        }
        int diffDay = fromDay -  toDay;
        if(diffDay > 0){
            sb.append(diffDay);
            sb.append("天");
        }else if(diffDay < 0){
            int toMaxDay = getMaxDay(toYear, toMonth);
            int result = toMaxDay - toDay + fromDay;
            sb.append(result);
            sb.append("天");
        }
        return sb.toString();
    }  
    
    
    
    /**
     * 判断是否为闰年
     *
     * @param year
     * @return boolean
     * @date 2020/7/8 16:07
     */
    public static boolean isLeapYear(double year){
        boolean leap;
        leap=(year%400==0||(year%100!=0&&year%4==0)) ;
        return leap;
    }
    /**
     * 获取当月最大一天
     *
     * @param year
     * @param month
     * @return int
     * @date 2020/7/8 16:07
     */
    public static int getMaxDay(double year,int month){
        int []M={31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year)&&month==2){
            return 29;
        }else{
            return M[month-1];
        }
    }

    
    
    public static void main(String[] args) {
        String str = calcTimeDiff("2020-12-01", "2023-12-08");
        System.out.printf(str);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值