【问题标题】:How to get list of dates between two dates? [duplicate]如何获取两个日期之间的日期列表? [复制]
【发布时间】:2013-07-17 06:24:04
【问题描述】:

在我的应用程序中,用户应该从listview 中选择日期。问题是生成这个列表。例如,我需要 2010-2013June-August 之间的所有日期(期间可能是 daymonth、年)。是否有任何方法可以获取该数据?

示例: 我需要 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

提前致谢

【问题讨论】:

标签: java android jodatime


【解决方案1】:

对于 list 你可以这样做:

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&lt;LocalDate&gt;,您可以编写自己的类来非常有效地完成此操作,而不是建立一个列表。如果您不介意一定程度的嵌套,您可以使用匿名类来做到这一点。例如(未经测试):

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

【讨论】:

  • 有没有办法迭代数周而不是数天?
  • @Amadan:正如 Amadan 所说。
  • 很好的答案。谢谢大佬
【解决方案2】:

像这样使用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;
}

干杯!

致谢:this

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多