得到兩個日期之間所有的日期

   /**

     * 得到兩個日期之間所有的日期

     *

     * @param begin 開始日期

     * @param end 結束日期

     * @return List<Date> 返回日期的List集合

     */
    public static List<Date> getBetweenDates(String beginStr, String endStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(dayFormat);
        Date begin = null;
        Date end = null;
        try {
            begin = sdf.parse(beginStr);
            end = sdf.parse(endStr);
        } catch (ParseException e) {
            throw new BusinessException(CodeUtil.FAIL, "日期格式轉換失敗" + e.getMessage());
        }

        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        while (begin.getTime() <= end.getTime()) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            begin = tempStart.getTime();
        }
        return result;
    }

說明:dayFormat爲定義的日期格式類型如:yyyy-MM-dd

發佈了13 篇原創文章 · 獲贊 40 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章