如何获取java中两个日期之间的日期列表?如何包含/排除开始日期/结束日期? [英] how to get a list of dates between two dates in java ? How to include/exclude start date/end date also?

查看:237
本文介绍了如何获取java中两个日期之间的日期列表?如何包含/排除开始日期/结束日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过堆栈溢出中给出的示例
如何获取java中两个日期之间的日期列表

I have tried the example given in stack overflow how to get a list of dates between two dates in java

代码完美无缺。但是有一个小问题。我的名单中也没有结束日期。如何选择包含/排除开始日期并包括结束日期?
那么,我是通过使用remove()和add()手动完成的,或者Joda API可以为我做这个吗?

The code works perfectly. But there is a small problem. I don't get the end date also in my List. How do I choose to include/exclude the start date and include the end date ? Do, I do that manually by using remove() and add() or can Joda API do that for me?

推荐答案

基于关于API ,似乎有没有直接的选择方式包括。

Based on API, it seems there is no direct way to choose include.

一个黑客可能是,只需在天数上加+1。

One hack may be, just add +1 to number of days.

List<LocalDate> dates = new ArrayList<LocalDate>();
int days = Days.daysBetween(startDate, endDate).getDays()+1;
for (int i=0; i < days; i++) {
    LocalDate d = startDate.withFieldAdded(DurationFieldType.days(), i);
    dates.add(d);
}

这篇关于如何获取java中两个日期之间的日期列表?如何包含/排除开始日期/结束日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆