java 根据开始和结束日期得到之间所有日期集合

来源:互联网 发布:怎样增加淘宝店铺流量 编辑:程序博客网 时间:2024/06/05 03:41
public static void main(String[] args) throws Exception {String start = "2014-01-03";String end = "2014-03-05";SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date dBegin = sdf.parse(start);Date dEnd = sdf.parse(end);List<Date> listDate = getDatesBetweenTwoDate(dBegin, dEnd);for(int i=0;i<listDate.size();i++){System.out.println(sdf.format(listDate.get(i)));}}/** * 根据开始时间和结束时间返回时间段内的时间集合 *  * @param beginDate * @param endDate * @return List */public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {List<Date> lDate = new ArrayList<Date>();lDate.add(beginDate);// 把开始时间加入集合Calendar cal = Calendar.getInstance();// 使用给定的 Date 设置此 Calendar 的时间cal.setTime(beginDate);boolean bContinue = true;while (bContinue) {// 根据日历的规则,为给定的日历字段添加或减去指定的时间量cal.add(Calendar.DAY_OF_MONTH, 1);// 测试此日期是否在指定日期之后if (endDate.after(cal.getTime())) {lDate.add(cal.getTime());} else {break;}}lDate.add(endDate);// 把结束时间加入集合return lDate;}

0 0
原创粉丝点击