0

I need to match a (partial) searchstring to a Date with timestamp from the database.

I have a searchbox that allows you to search through 4 values at once. Those being: code, date, name and code2. When you input a value I make a new List with all the valid searchresults. At the moment everything works except my date, I can search on the independent numbers but when I use the following format I get a 404.

("dd/MM/yyyy")

Now I do have a formatter in place to transform my Date from the database (format = "yyyy-mm-dd hh:mm:ss") into a String. However I keep having the same problem. The moment I enter / into the searchbox it won't generate my list anymore and I get the 404.

And I was wondering what the best solution would be? Transform everything into a String, transform the searchstring into a Date format + 00:00:00 or change something in the searchbox so that it only looks for a date if the format matches exactly?

  if (orderHeader.getCode() != null) {

 orderHeaderDTO.setCode(orderHeader.getCode());
 }

  if (orderHeader.getOrderDate() != null) {

      while (searchString.contains("/")) {
      Date date = orderHeader.getOrderDate();
      String dateString = DateUtil.convertDateToString(date);

 if (dateString == searchString) {

       orderHeaderDTO.setOrderDate(date);
    }       
 }

able to search with the following format = "dd/MM/yyyy" & not get a 404 or a 500 error

Chim_S
  • 1
  • 2
  • I’d probably do it the other way around, parse the search string into a `LocalDateTime` and compare it to the date and time of the order. I would definitely avoid the `Date` class, it’s poorly designed and long outdated, and opt for [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) instead. – Ole V.V. May 15 '19 at 14:39

1 Answers1

0

The problem is in the comparison of two string objects. == operator will only check if the references are equaly, In this case it is not.

equals method is all you need to use,

 dateString.equals(searchString)

I couldnt comment anything on your DateUti.convertDateToString(date) as you have not shared the code.

Prabhu
  • 129
  • 1
  • 1
  • 9