Calculate the difference in months + days between two dates

For example, there is a difference of 9 days in June between 2018-04-05 and 2018-10-14;
directly upload the code below

  /**
     * 计算连个日期相差多少月+天
     *
     * @param stDate
     * @param endDate
     * @return
     */
    public static String getDayAndMonth(String stDate, String endDate) {
        int month = 0, day = 0;
        try {
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(DateUtil.stringToDate(endDate, "yyyy-MM-dd"));
            int enDay = endCalendar.get(Calendar.DATE);

            Calendar lastMonthCalendar = Calendar.getInstance();
            lastMonthCalendar.setTime(DateUtil.stringToDate(endDate, "yyyy-MM-dd"));
            lastMonthCalendar.set(Calendar.MONTH, endCalendar.get(Calendar.MONTH) - 1);
            int dayOfMonth = lastMonthCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

            Calendar stCalendar = Calendar.getInstance();
            stCalendar.setTime(DateUtil.stringToDate(stDate, "yyyy-MM-dd"));

            if (stCalendar.after(endCalendar)) {
                return "起始日期大约结束日期";
            }

            int stDay = stCalendar.get(Calendar.DATE);
            month = DateUtil.getMonthSpace(stDate, endDate);

            if (enDay < stDay) {
                day = dayOfMonth - stDay + enDay;
                month = month - 1;
            } else {
                day = enDay - stDay;
            }
            return "相差" + month + "月" + day + "天";
        } catch (ParseException e) {
            e.printStackTrace();
            return "计算失败";
        }

    }

   /**
     * 计算两个日期相差多少月
     * @param stDate <String>
     * @param endDate <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(String stDate, String endDate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar bef = Calendar.getInstance();
            Calendar aft = Calendar.getInstance();
            bef.setTime(sdf.parse(stDate));
            aft.setTime(sdf.parse(endDate));
            int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
            int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
            return Math.abs(month + result);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

    }
  // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒,
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

How to use: System.out.println("计算结果:"+getDayAndMonth("2018-04-29","2018-10-14"));
console output:计算结果:相差5月15天

Guess you like

Origin blog.csdn.net/fzkf9225/article/details/83114429