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

/**
     * 获取两个时间年月日时间差
     *
     * @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);
    }

你可能感兴趣的:(java)