Using java.time
The Joda-Time project is now in maintenance mode , with the team advising switching to the java.time classes. See the tutorial from Oracle .
The LocalDate class represents a date value only without time and without a time zone.
LocalDate start = LocalDate.of( 2017 , Month.JANUARY , 23 ) ; LocalDate stop = LocalDate.of( 2017 , Month.FEBRUARY , 2 ) ;
By the way, you can add a health check to make sure that the ending is not up to the start.
Not after
I believe that the logic you are looking to include the end date is "not after." The LocalDate class includes the isAfter method, to which you can add the logical "NOT" ( ! ).
In addition, the while seems more appropriate and self-evident in this situation than the for loop.
LocalDate ld = start ; List<LocalDate> dates = new ArrayList<>() ; while ( ! ld.isAfter( stop ) ) { dates.add( ld );
See this code run on IdeOne.com .
dates: [2017-01-23, 2017-01-24, 2017-01-25, 2017-01-26, 2017-01-27, 2017-01-28, 2017-01-29, 2017-01-30 , 2017-01-31, 2017-02-01, 2017-02-02]
Half open
iteration stops when currentDate reaches the day before endDate
This is really desirable. Known as Half-Open, the general approach to handling date and time is to treat the beginning as inclusive, while the ending is exceptional. Thus, the lunch break begins at 12:00 (noon) and comes up, but does not include, 13:00:00 (13:00). January starts on January 1 and ends, but does not include February 1. The week starts on Monday and passes, but does not include the following Monday. Most useful, this approach avoids the problem of determining the last split-second date-time when some systems use milliseconds (x.999), some (x.999999), the same nanoseconds (x.999999999), and others use Variations such as 5 decimal places (x.99999). Instead, we approach, but do not include, the first moment of the next hour or day, etc.
I find that using the Half-Open method consistently throughout my code makes the code easier to read, easier to understand, and much less likely to cause errors in turn. I was caught in countless financial secrecy issues that turned out to be confusion or misunderstanding regarding a report covering a date with inclusive and exclusive dates. Therefore, if possible, educate your users to think consistently about Half-Open. If this is not possible, then adjust your code so that your logic and loops at least use Half-Open.
Here is code similar to the above, but using isBefore , and NOT isAfter , use the Half-Open approach. The ending is February 3, not February 2.
LocalDate start = LocalDate.of( 2017 , Month.JANUARY , 23 ) ; LocalDate stop = LocalDate.of( 2017 , Month.FEBRUARY , 3 ) ; // Third instead of the Second of February, to be half-open. LocalDate ld = start ; List<LocalDate> dates = new ArrayList<>() ; while ( ld.isBefore( stop ) ) { // Using "isBefore" for Half-Open approach. dates.add( ld ); // Collect this date. ld = ld.plusDays( 1 ) ; // Setup the next loop. }
See this code run on IdeOne.com .
start: 2017-01-23 | stop: 2017-02-03
dates: [2017-01-23, 2017-01-24, 2017-01-25, 2017-01-26, 2017-01-27, 2017-01-28, 2017-01-29, 2017-01-30 , 2017-01-31, 2017-02-01, 2017-02-02]
About java.time
The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .
The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .