How to Get a List of Dates Between Two Dates in Java

Get the list of dates between two dates

You should try using Calendar, which will allow you to walk from one date to another...

Date fromDate = ...;
Date toDate = ...;

System.out.println("From " + fromDate);
System.out.println("To " + toDate);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.getTime().before(toDate)) {
cal.add(Calendar.DATE, 1);
System.out.println(cal.getTime());
}

Updated

This example will include the toDate. You can rectify this by creating a second calendar that acts as the lastDate and subtracting a day from it...

Calendar lastDate = Calendar.getInstance();
lastDate.setTime(toDate);
lastDate.add(Calendar.DATE, -1);

Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.before(lastDate)) {...}

This will give you all the dates "between" the start and end dates, exclusively.

Adding Dates to an ArrayList

List<Date> dates = new ArrayList<Date>(25);
Calendar cal = Calendar.getInstance();
cal.setTime(fromDate);
while (cal.getTime().before(toDate)) {
cal.add(Calendar.DATE, 1);
dates.add(cal.getTime());
}

2018 java.time Update

Time moves on, things improve. Java 8 introduces the new java.time API which has superseded the "date" classes and should, as a preference, be used instead

LocalDate fromDate = LocalDate.now();
LocalDate toDate = LocalDate.now();

List<LocalDate> dates = new ArrayList<LocalDate>(25);

LocalDate current = fromDate;
//current = current.plusDays(1); // If you don't want to include the start date
//toDate = toDate.plusDays(1); // If you want to include the end date
while (current.isBefore(toDate)) {
dates.add(current));
current = current.plusDays(1);
}

Generating all days between 2 given dates in Java

Take a look at JodaTime: http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html

DateTime dateTime1 = new DateTime(date1);
DateTime dateTime2 = new DateTime(date2);

List<Date> allDates = new ArrayList();

while( dateTime1.before(dateTime2) ){
allDates.add( dateTime1.toDate() );
dateTime1 = dateTime1.plusDays(1);
}

Java 8 LocalDate - How do I get all dates between two dates?

Assuming you mainly want to iterate over the date range, it would make sense to create a DateRange class that is iterable. That would allow you to write:

for (LocalDate d : DateRange.between(startDate, endDate)) ...

Something like:

public class DateRange implements Iterable<LocalDate> {

private final LocalDate startDate;
private final LocalDate endDate;

public DateRange(LocalDate startDate, LocalDate endDate) {
//check that range is valid (null, start < end)
this.startDate = startDate;
this.endDate = endDate;
}

@Override
public Iterator<LocalDate> iterator() {
return stream().iterator();
}

public Stream<LocalDate> stream() {
return Stream.iterate(startDate, d -> d.plusDays(1))
.limit(ChronoUnit.DAYS.between(startDate, endDate) + 1);
}

public List<LocalDate> toList() { //could also be built from the stream() method
List<LocalDate> dates = new ArrayList<> ();
for (LocalDate d = startDate; !d.isAfter(endDate); d = d.plusDays(1)) {
dates.add(d);
}
return dates;
}
}

It would make sense to add equals & hashcode methods, getters, maybe have a static factory + private constructor to match the coding style of the Java time API etc.

How to get list of dates between two dates?

For a list you could just do:

public static List<LocalDate> datesBetween(LocalDate start, LocalDate end) {
List<LocalDate> ret = new ArrayList<LocalDate>();
for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
ret.add(date);
}
return ret;
}

Note, that will include end. If you want it to exclude the end, just change the condition in the loop to date.isBefore(end).

If you only need an Iterable<LocalDate> you could write your own class to do this very efficiently rather than building up a list. You could do this with an anonymous class, if you didn't mind a fair degree of nesting. For example (untested):

public static Iterable<LocalDate> datesBetween(final LocalDate start,
final LocalDate end) {
return new Iterable<LocalDate>() {
@Override public Iterator<LocalDate> iterator() {
return new Iterator<LocalDate>() {
private LocalDate next = start;

@Override
public boolean hasNext() {
return !next.isAfter(end);
}

@Override
public LocalDate next() {
if (next.isAfter(end)) {
throw NoSuchElementException();
}
LocalDate ret = next;
next = next.plusDays(1);
return ret;
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}

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

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);
}

Getting a list of dates between two dates in java different results

try like this once:

while (!start.isAfter(end)) {
totalDates.add(start);
Milestones modelMilestones = new Milestones();
modelMilestones .setMilestone(start.toString("MMMM dd, yyyy"));
mDataList.add(modelMilestones);

start = start.plusDays(1);
}


Related Topics



Leave a reply



Submit