How to get a list of dates between two dates in java? How to include / exclude the start and end date of a date?

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

The code works fine. But there is a small problem. I do not get the end date also on my list. How to choose the inclusion / exclusion of the start date and the inclusion of the end date? Do I do it manually using remove () and add () or can the Joda API do this for me?

+2
source share
2 answers

Based on the API, there seems to be no direct choice.

One hack may be, just add +1 to the 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); } 
+5
source

Using java.time

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

half open

Working with a date usually uses the Half-Open method to determine the amount of time when the beginning is inclusive and the ending is exceptional. I believe that the consistent use of this approach both in your code and in your user business makes life easier, avoiding inaccuracies that can lead to misunderstandings or errors.

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

 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 including this end date, add an entry day.

 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 ! isAfter and (B) add it 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 is simplified in Java 9, where you can get the Stream<LocalDate from LocalDate::datesUntil . This call returns a sequential ordered stream of dates.

A variant of this method allows you to specify a quantity for increasing dates. So, for example, instead of defaulting a one-time gain, you can increase by two to get it every day.


About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy datetime 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 .

+1
source

All Articles