【问题标题】:Search arraylist for LocalDateTime range在数组列表中搜索 LocalDateTime 范围
【发布时间】:2018-11-18 13:06:57
【问题描述】:

在用户输入后需要一些代码帮助以从 arrayList 中返回日期范围。

ArrayList mainList

static ArrayList<LEntry> mainList = new ArrayList();

public LEntry(LocalDateTime date, String id)
    2018-10-23T12:05:22 A2315
    2017-01-13T10:09:30 A2253
    2018-05-10T18:55:30 V1225
    2018-05-03T17:44:50 R9952
    2017-06-15T16:25:31 A2253

我不成功的代码:

private static ArrayList<LogEntry> range() {
    String sDate, eDate;
    LocalDateTime startDate, endDate;

    System.out.println("Start date:\n");
    sDate = in.nextLine().replace(" ", "T");
    System.out.println("End date:\n");
    eDate = in.nextLine().replace(" ", "T");

    //System.out.println(eDate);
    startDate = LocalDateTime.parse(sDate, DateTimeFormatter.ISO_DATE_TIME);
    endDate = LocalDateTime.parse(eDate, DateTimeFormatter.ISO_DATE_TIME);

    mainList.stream().filter(i -> i.date ?? ((startDate.isBefore(i.date))&&(endDate.isAfter(i.date))).forEach(j -> Ranges.add(0, j));

    return Ranges;
}

我正在尝试使用 isBefore 和 isAfter 来获取我的范围,但由于它们返回 true/false,我在将其集成到我的代码中时遇到了一些问题。

我的目标是在给定日期之间获取 LEntries。
感谢您的任何帮助。

已解决,谢谢@user7

mainList.stream()
    .filter(i -> startDate.isBefore(i.date) && endDate.isAfter(i.date))
    .forEach(j -> Ranges.add(0, j));

【问题讨论】:

  • 很高兴您在另一个 Stack Overflow 用户的帮助下解决了这个问题。您无需在问题中发布解决方案。其他用户不会在那里寻找,他们宁愿在下面的 1 个答案 部分中寻找。

标签: java datetime arraylist date-comparison localdate


【解决方案1】:

你快到了

 mainList.stream()
        .filter(i -> startDate.isBefore(i.date) && endDate.isAfter(i.date))
        .forEach(i -> {
             //your code
            });

【讨论】:

  • 感谢您的帮助和客气话。试图使事情复杂化......再次。
猜你喜欢
  • 2019-01-22
  • 2016-02-03
  • 2015-07-06
  • 2018-10-13
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 2015-10-06
  • 2016-03-29
相关资源
最近更新 更多