2

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

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?

Community
  • 1
  • 1
bread butter
  • 617
  • 3
  • 10
  • 18
  • This question makes no sense without some discrete time step. Do you want the dates between them that change in seconds? hours? – obataku Aug 23 '12 at 00:07
  • As far as I know, my library [Time4J](http://time4j.net/javadoc-en/net/time4j/range/DateInterval.html) is the only API with configurable inclusive or exclusive interval boundaries. In other APIs like Joda-Time, you have first to add a day to make an exclusive end date inclusive. – Meno Hochschild Feb 20 '17 at 12:25

2 Answers2

6

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

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);
}
kosa
  • 65,990
  • 13
  • 130
  • 167
  • While your answer satisfies OP problem, IMO this question shows tons of lack of effort from him/her. – Luiggi Mendoza Aug 23 '12 at 00:12
  • @LuiggiMendoza: sort of agree. Well, I couldn't help anything for that except voting for close. – kosa Aug 23 '12 at 00:14
  • 2
    I am new to the huge Joda API. So, i wanted to know if someone already knew the answer to that. – bread butter Aug 23 '12 at 00:20
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 20 '17 at 07:07
1

Using java.time

The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.

Half-open

Date-time work commonly uses the Half-Open approach to defining a span of time where the beginning is inclusive and the ending is exclusive. I believe consistent use of this approach in both your code and in your users’ business makes life much easier, avoiding amibiguities that can result in misunderstandings or errors.

The java.time classes use the Half-Open approach. Keep that in mind when using Period, Duration, and ChronUnit classes.

LocalDate start = LocalDate.of( 2017 , 1 , 23 );
LocalDate stop = LocalDate.of( 2017 , 2 , 14 );

List<LocalDate> dates = new ArrayList<>( (int) ChronoUnit.DAYS.between( start , stop ) ) ; 
LocalDate ld = start ;
while( ld.isBefore( stop ) ) {  // Half-open approach, ending is exclusive.
    dates.add( ld );
    // Set up the next loop.
    ld = ld.plusDays( 1 );
}

If you insist on that ending date to be inclusive, add a day to the input.

LocalDate stop = LocalDate.of( 2017 , 2 , 14 ).plusDays( 1 );  // Effectively making ending date inclusive.

…or (A) change the logic of the while test from isBefore to ! isAfter, and (B) add one to the initial capacity of the ArrayList.

List<LocalDate> dates = new ArrayList<>( ( (int) ChronoUnit.DAYS.between( start , stop ) ) + 1 ) ; // Add one to accommodate inclusive ending (*not* Half-Open).
LocalDate ld = start ;
while( ! ld.isAfter( stop ) ) {  // Ending is inclusive (*not* Half-Open). So using "not after" logic rather than "is before". 
    dates.add( ld );
    // Set up the next loop.
    ld = ld.plusDays( 1 );
}

Stream<LocalDate>

By the way, this code gets simpler in Java 9 where you can get a Stream<LocalDate from LocalDate::datesUntil method. That call returns a sequential ordered stream of dates.

A variant of that method lets you specify an amount to increment the dates. So, for example, instead of the default of single day increment, you can increment by two to get every-other-day.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154