如何获取两个日期之间的日期列表?

3
在我的应用中,用户应该从列表视图中选择日期。问题是如何生成这个列表。例如,我需要在2010-2013六月-八月之间所有的日期(可能是)。是否有一种方法可以获取这些数据?
示例: 我需要2013年1月1日-2013年1月10日之间的日期。
- 01.01.2013 - 02.01.2013 - 03.01.2013 - 04.01.2013 - 05.01.2013 - 06.01.2013 - 07.01.2013 - 08.01.2013 - 09.01.2013 - 10.01.2013
提前感谢。

3
一个简单的搜索给了我这个链接:http://stackoverflow.com/questions/12083053/how-to-get-a-list-of-dates-between-two-dates-in-java-how-to-include-exclude-st - Jimmy Geers
2个回答

5

对于一个列表,你可以这样做:

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

请注意,这将包含end。如果你想要排除结尾,只需在循环中更改条件为date.isBefore(end)
如果您只需要一个Iterable<LocalDate>,您可以编写自己的类以非常高效地完成此操作,而不是建立列表。如果您不介意相当程度的嵌套,可以使用匿名类来实现。例如(未经测试):
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();
                }
            };
        }
    };
}

有没有一种方法可以迭代周而不是天? - Avan
@Amadan:正如Amadan所说。 - Jon Skeet
非常好的回答。谢谢你。 - Naween Banuka

3

像这样使用DatePicker碎片:

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

}

首先获取日期应该更加容易。使用以下代码来获取日期范围:

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

干杯!

致谢:此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接