Java把一个时间范围内每天的日期放入到容器中

一、代码如下:

/**
	 *  一个开始时间一个结束时间,把每天的日期放到容器中
	 * @param dBegin
	 * @param dEnd
	 * @return
	 * @throws ParseException
	 */
    public static List<String> findDates(String dBegin, String dEnd) throws ParseException{
        //日期工具类准备
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 
        //设置开始时间
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(format.parse(dBegin));
 
        //设置结束时间
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(format.parse(dEnd));
 
        //装返回的日期集合容器
        List<String> Datelist = new ArrayList<String>();
        //将第一个月添加里面去
        Datelist.add(format.format(calBegin.getTime()));
        // 每次循环给calBegin日期加一天,直到calBegin.getTime()时间等于dEnd
        while (format.parse(dEnd).after(calBegin.getTime()))  {
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            Datelist.add(format.format(calBegin.getTime()));
        }
        return Datelist;
    }

二、测试类:

public static void main(String[] args) throws Exception {
		List<String> findDates = DataVerification.findDates("2018-11-01","2018-11-25");
		System.out.println(findDates);
	}

三、返回数据:

[2018-11-01, 2018-11-02, 2018-11-03, 2018-11-04, 2018-11-05, 2018-11-06, 2018-11-07, 2018-11-08, 2018-11-09, 2018-11-10, 2018-11-11, 2018-11-12, 2018-11-13, 2018-11-14, 2018-11-15, 2018-11-16, 2018-11-17, 2018-11-18, 2018-11-19, 2018-11-20, 2018-11-21, 2018-11-22, 2018-11-23, 2018-11-24, 2018-11-25]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值