获取2个日期之间的月份

  /**
     * 获取2个日期之间的月份
     * @param beginDate
     * @param endDate
     * @param dateList
     * @return
     */
    public static List<Date> getDifferentMonthBetweenDate(Date beginDate, Date endDate, List<Date> dateList) {
        beginDate = DateTimeUtil.parseDatetime2Date(beginDate);
        endDate = DateTimeUtil.parseDatetime2Date(endDate);
        if (DateTimeUtils.compareDate(beginDate, endDate) == 1) {
            return null;
        }
        Calendar beginCal = Calendar.getInstance();
        beginCal.setTime(beginDate);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);

        while (DateTimeUtils.compareDate(beginCal.getTime(), endCal.getTime()) < 1) {
            dateList.add(beginCal.getTime());
            beginCal.add(Calendar.MONTH, 1);
        }

        return dateList;
    }


  
  /**
  * dateTime 转化为 date
  * @param date
  * @return
  */
  public static Date parseDatetime2Date(Date date) {
  return DateTimeUtils.parseStrToDate(DateTimeUtils.formateDateToStr(date, DateTimeUtils.FORMAT_YMD),
  DateTimeUtils.FORMAT_YMD);
  }
 
posted @ 2020-01-19 11:22  二次元的程序猿  阅读(473)  评论(0编辑  收藏  举报