如何检查Java集合Arraylist中的日期部分?

如何检查Java集合Arraylist中的日期部分?,list,arraylist,collections,List,Arraylist,Collections,我有一个ArrayList: List<Date> date= ArrayList<Date>(); date.add(2017-07-26 09:27:33); date.add(2017-07-28 10:11:33); date.add(2017-07-25 08:27:33); date.add(2017-07-25 07:27:33); 我想检查的基础上,只有唯一的日期,而不是时间。我怎样才能只检查日期而不是时间的基数呢?首先,您的代码无法编译 让我们将其更改

我有一个ArrayList:

List<Date> date= ArrayList<Date>();
date.add(2017-07-26 09:27:33);
date.add(2017-07-28 10:11:33);
date.add(2017-07-25 08:27:33);
date.add(2017-07-25 07:27:33);

我想检查的基础上,只有唯一的日期,而不是时间。我怎样才能只检查日期而不是时间的基数呢?

首先,您的代码无法编译

让我们将其更改为有效的java代码,并只比较两个日期,而不比较时间部分:

    List<Date> date = new ArrayList();
    DateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH);
    DateFormat compareFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);

    try {
        date.add(format.parse("2017-07-26 09:27:33"));
        date.add(format.parse("2017-07-28 10:11:33"));
        date.add(format.parse("2017-07-25 08:27:33"));
        date.add(format.parse("2017-07-25 07:27:33"));

        Date searchedDate = format.parse("2017-07-26 16:27:33");

        boolean isExist = date.stream().anyMatch(d -> compareFormat.format(d).equals(compareFormat.format(searchedDate)));


        System.out.println(isExist);

    } catch (ParseException e) {
        e.printStackTrace();
    }
List date=new ArrayList();
DateFormat格式=新的SimpleDataFormat(“yyyy-mm-dd hh:mm:ss”,Locale.ENGLISH);
DateFormat compareFormat=新的SimpleDateFormat(“yyyy-mm-dd”,Locale.ENGLISH);
试一试{
添加日期(格式解析(“2017-07-2609:27:33”);
添加日期(格式解析(“2017-07-28 10:11:33”);
添加日期(格式解析(“2017-07-25 08:27:33”);
添加日期(格式解析(“2017-07-25 07:27:33”);
日期searchedDate=format.parse(“2017-07-26 16:27:33”);
布尔值isExist=date.stream().anyMatch(d->compareFormat.format(d).equals(compareFormat.format(searchedDate));
System.out.println(isExist);
}捕获(解析异常){
e、 printStackTrace();
}

另外还有另外一种解决方案可以比较没有时间部分的日期,看看另一种解决方案:

在我看来,这不像是有效的Java代码。请提取一个。无论如何,匹配的定义非常简单,所以为什么不简单地编写代码来扫描列表中的一个呢?我关心的是我可以只比较日期而不是时间。我只是想要一个方法。验证代码不是必需的。请阅读-总结是,这不是解决志愿者问题的理想方法,并且可能会对获得答案产生反作用。请不要在你的问题中添加这一点。@halfer可能会有警告,但你把我变成了否定。这非常可悲。@Bachas:请不要养成猜测谁否决了你的问题的习惯。这里的投票是匿名的,除非人们说他们是否投票以及如何投票。
    List<Date> date = new ArrayList();
    DateFormat format = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH);
    DateFormat compareFormat = new SimpleDateFormat("yyyy-mm-dd", Locale.ENGLISH);

    try {
        date.add(format.parse("2017-07-26 09:27:33"));
        date.add(format.parse("2017-07-28 10:11:33"));
        date.add(format.parse("2017-07-25 08:27:33"));
        date.add(format.parse("2017-07-25 07:27:33"));

        Date searchedDate = format.parse("2017-07-26 16:27:33");

        boolean isExist = date.stream().anyMatch(d -> compareFormat.format(d).equals(compareFormat.format(searchedDate)));


        System.out.println(isExist);

    } catch (ParseException e) {
        e.printStackTrace();
    }