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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17692575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:41:58  来源:igfitidea点击:

How to get list of dates between two dates?

javaandroidjodatime

提问by Avan

In my application user should select date from listview. The problem is generating this list. For example I need all dates between 2010-2013or June-August(period maybe day, month, year). Is there any method that allows to get that data?

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

Example: I need dates between 01.01.2013 - 10.01.2013

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

提前致谢

回答by Jon Skeet

For a listyou 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 excludethe end, just change the condition in the loop to date.isBefore(end).

请注意,这将包括end. 如果您希望它排除结尾,只需将循环中的条件更改为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):

如果您只需要一个,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();
                }
            };
        }
    };
}

回答by curlyreggie

Use a DatePickerFragment like this:

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

}

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

学分:这个