Skip to content Skip to sidebar Skip to footer

How To Get List Of Dates Between Two Dates?

In my application user should select date from listview. The problem is generating this list. For example I need all dates between 2010-2013 or June-August (period maybe day, month

Solution 1:

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):

publicstaticIterable<LocalDate> datesBetween(final LocalDate start,
                                               final LocalDate end) {
    returnnewIterable<LocalDate>() {
        @OverridepublicIterator<LocalDate> iterator() {
            returnnewIterator<LocalDate>() {
                privateLocalDate next = start;

                @OverridepublicbooleanhasNext() {
                    return !next.isAfter(end);
                }

                @OverridepublicLocalDatenext() {
                    if (next.isAfter(end)) {
                        throwNoSuchElementException();
                    }
                    LocalDate ret = next;
                    next = next.plusDays(1);
                    return ret;
                }

                @Overridepublicvoidremove() {
                    thrownewUnsupportedOperationException();
                }
            };
        }
    };
}

Solution 2:

Use a DatePicker Fragment like this:

privatestaticclassDatePickerFragmentextendsDialogFragmentimplementsDatePickerDialog.OnDateSetListener {

    @Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the pickerfinalCalendarc= Calendar.getInstance();
        intyear= c.get(Calendar.YEAR);
        intmonth= c.get(Calendar.MONTH);
        intday= c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return itreturnnewDatePickerDialog(getActivity(), this, year, month, day);
    }

    @OverridepublicvoidonDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // Copy the Date to the EditText set.
        dateValue = String.format("%04d", year) + "-" + String.format("%02d", monthOfYear + 1) + "-" + String.format("%02d", dayOfMonth);
    }

}

This should be easier to get dates in the first place. Use the below code for Date Ranges:

publicstaticList<Date> dateInterval(Date initial, Date final) {
     List<Date> dates = newArrayList<Date>();
     Calendar calendar = Calendar.getInstance();
     calendar.setTime(initial);

     while (calendar.getTime().before(final)) {
         Date result = calendar.getTime();
         dates.add(result);
         calendar.add(Calendar.DATE, 1);
     }

return dates;
}

Cheers!

Credits: this

Post a Comment for "How To Get List Of Dates Between Two Dates?"