如何获取给定日期范围内的日期列表?

时间:2016-11-17 08:39:05

标签: java

我正在尝试以iso格式(" yyyy-MM-dd")获取日期范围之间的日期列表,如下所示:

ArrayList<String> datesList = new ArrayList<String>();
try {
    SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");
    int[] fromDateParts = new int[3];
    for (int f = 0; f < fromIsoDate.split("-").length; f++) {
        fromDateParts[f] = Integer.parseInt(fromIsoDate.split("-")[f]);
        System.out.println("fromDateParts[" + f + "] : " + fromDateParts[f]);
    }
    int[] toDateParts = new int[3];
    for (int t = 0; t < toIsoDate.split("-").length; t++) {
        toDateParts[t] = Integer.parseInt(toIsoDate.split("-")[t]);
        System.out.println("toDateParts[" + t + "] : " + toDateParts[t]);
    }
    Calendar fromDate = new GregorianCalendar(fromDateParts[0], fromDateParts[1], fromDateParts[2]);
    Calendar toDate = new GregorianCalendar(toDateParts[0], toDateParts[1], toDateParts[2]);
    System.out.println("fromDate " + fromDate + " toDate " + toDate);
    boolean hasMoreDays = true;
    while (hasMoreDays) {
        if ((fromDate.before(toDate)) || (fromDate.equals(toDate))) {
            datesList.add(isoFormat.format(fromDate));
            fromDate.add(Calendar.DAY_OF_MONTH, 1);
        } else {
            hasMoreDays = false;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
    System.out.println("Exception : While determining the dates in between " + fromIsoDate + " and " + toIsoDate + "!");
}

但是,它会引发错误:
java.lang.IllegalArgumentException:无法将给定的Object格式化为日期。

请指导我获取日期范围之间的日期列表!

3 个答案:

答案 0 :(得分:1)

只需将您的开始日期换成Calendar并使用:

Date startDate;
Date endDate;

Calendar c = Calendar.getInstance(); 
c.setTime(startDate); 
Date dt = startDate;
List<Date> dates = new ArrayList<>();

do {
    dates.add(dt);
    c.add(Calendar.DATE, 1);
    dt = c.getTime();
} while (endDate.getTime() > dt.getTime());

正如您所期望的那样,Java 8 API或者可能使用JodaTime比使用Java 7日期方式更容易。

答案 1 :(得分:1)

您应该将Date传递给SimpleDateFormat.fotmat,而不是Calendar

datesList.add(isoFormat.format(fromDate));

datesList.add(isoFormat.format(fromDate.getTime));

以下是我的解决方案:

    String fromStr = "2016-11-01";
    String toStr = "2016-11-17";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar from = Calendar.getInstance();
    from.setTime(sdf.parse(fromStr));
    Calendar to = Calendar.getInstance();
    to.setTime(sdf.parse(toStr));

    List<Date> retval = new ArrayList<Date>();
    while (from.before(to) || from.equals(to)) {
        retval.add(from.getTime());
        System.out.println(sdf.format(from.getTime()));
        from.add(Calendar.DATE, 1);
    }

希望这有帮助!

答案 2 :(得分:0)

如果您使用java8,您可以将整个内容写在一行中,如下所示,我使用过java.util.Date

        List<Date> dates = new ArrayList<>();

        Date fromDate = new Date(2016, 11, 02);
        Date toDate = new Date(2016, 11, 28);

        dates.add(new Date(2016, 11, 01));
        dates.add(new Date(2016, 11, 02));
        dates.add(new Date(2016, 11, 03));
        dates.add(new Date(2016, 11, 04));

        List<Date> filteredDates = dates.stream()
                .filter((e) -> e.after(fromDate) && e.before(toDate)).collect(Collectors.toList());