3

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, year). Is there any method that allows to get that data?

Example: I need dates between 01.01.2013 - 10.01.2013

  1. 01.01.2013
  2. 02.01.2013
  3. 03.01.2013
  4. 04.01.2013
  5. 05.01.2013
  6. 06.01.2013
  7. 07.01.2013
  8. 08.01.2013
  9. 09.01.2013
  10. 10.01.2013

Thanks in advance

ridoy
  • 6,274
  • 2
  • 29
  • 60
Avan
  • 237
  • 1
  • 3
  • 8
  • 3
    A simple search gave me this: http://stackoverflow.com/questions/12083053/how-to-get-a-list-of-dates-between-two-dates-in-java-how-to-include-exclude-st – Jimmy Geers Jul 17 '13 at 06:26

2 Answers2

5

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();
                }
            };
        }
    };
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

Use a DatePicker Fragment like this:

private static class DatePickerFragment extends DialogFragment 
                                     implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

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

    @Override
    public void onDateSet(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:

public static List<Date> dateInterval(Date initial, Date final) {
     List<Date> dates = new ArrayList<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

Community
  • 1
  • 1
curlyreggie
  • 1,530
  • 4
  • 21
  • 31