LocalDate比较,获取两个日期之间的订单 [英] LocalDate comparison, getting orders between two dates

查看:1153
本文介绍了LocalDate比较,获取两个日期之间的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public List< WorkItem> getWorkItemsByDate(String startDate,String endDate)throws ParseException {
LocalDate parsedStartDate = LocalDate.parse(startDate);
LocalDate parsedEndDate = LocalDate.parse(endDate);
return workItemRepository.findAll()。stream()。filter(w - > w.getUpdateDate()。isAfter(parsedStartDate)&& $ amp;
w.getUpdateDate()。isBefore(parsedEndDate))
.collect(Collectors.toList());
}

我有两个我想比较的日期并找到所有的工作项目有LocalDate)的日期。
我有一个问题,但我不知道如何检查相同的日期。
当我在代码中运行一个日期时,它会正常工作,直到您写入该项目的创建日期,那么它不起作用。



我如何使这个工作与说2018-05-28 - 2018-05-28,如果项目创建在这一天它不会在我的lambda。

解决方案

如果当天是同一天,则不会在此之前或之后发生,因此在这些情况下它将返回false。

您可以替换调用 compareTo 并检查 isAfter isBefore int 返回值。

  .filter(w  - > w.getUpdateDate()。compareTo(parsedStartDate) > = 0&& w.getUpdateDate()。compareTo(parsedEndDate)< = 0)


    public List<WorkItem> getWorkItemsByDate(String startDate, String endDate) throws ParseException {
    LocalDate parsedStartDate = LocalDate.parse(startDate);
    LocalDate parsedEndDate = LocalDate.parse(endDate);
    return workItemRepository.findAll().stream().filter(w -> w.getUpdateDate().isAfter(parsedStartDate) &&
                                                w.getUpdateDate().isBefore(parsedEndDate))
                                                .collect(Collectors.toList());
}

I have two dates that I want to compare between and find all the workitems(has LocalDate) for the dates. I have one problem though I can't figure out how to check for the same date. When you run a date in my code it works fine untill you write the date that the item was created, then it does not work.

How do I make this work with say 2018-05-28 - 2018-05-28, if the items were created on this day it will not work in my lambda.

解决方案

If the day is the same day it is not before, nor after, so it will return false in these cases.

You can replace the isAfter and isBefore with a call to compareTo and check the int return value. Less clear in code, but still understandable.

.filter(w -> w.getUpdateDate().compareTo(parsedStartDate) >= 0 && w.getUpdateDate().compareTo(parsedEndDate) <= 0)

这篇关于LocalDate比较,获取两个日期之间的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆