-1

I have one 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);

Now I am testing

date.contains(2017-07-25 11:27:33);     //should come true

I want to check on the basis only the only date not time. How can I check only base of date not time?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bachas
  • 233
  • 6
  • 16
  • That doesn't look like valid Java code to me. Please extract a [mcve]. Anyhow, the definition of a match is pretty simple, so why not simply write the code that scans the list for one? – Ulrich Eckhardt May 11 '18 at 06:35
  • my concern know can i compare date only not their time. I just want a approach . validate code not required. – Bachas May 11 '18 at 06:48
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 11 '18 at 07:24
  • @halfer there could be warning but you put me into negative.Its very sad. – Bachas May 14 '18 at 04:51
  • @Bachas: please do not get into the habit of guessing who has downvoted your question. Voting is anonymous here, except in the cases where people say if and how they voted. – halfer May 14 '18 at 07:33

1 Answers1

1

First of all your code can not compile.

Lets change it to valid java code and compare two date only without time portion:

    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();
    }

Also there are another solutions to compare date without time part, take a look another solutions : How to compare two Dates without the time portion?

Emre Savcı
  • 3,034
  • 2
  • 16
  • 25