Skip to content
Advertisement

HttpServer Request get date range from query string

I am new to Java and Vertx and I have a query string with the following format:

GET /examples/1/data?date_1[gt]=2021-09-28&date_1[lt]=2021-10-28

Here I have this date_1 parameter which is within a certain range. I have been using HttpServerRequest class to extract simple parameters like integers but not sure how to proceed with these kind of range parameters.

With the simple parameters, I can do something like:

String param = request.getParam(paramName);
paramAsInteger = Integer.valueOf(paramAsString);

However, confused as to how to deal with the gt and lt options and the fact that we have same parameter twice.

Advertisement

Answer

You say that you have difficulties parsing out these tokens. Here’s how you can handle this.

  1. The first thing to understand is that the parameter name is NOT “date1”

  2. There are actually two parameters here 2.1. “date_1[gt]” with a value of “2021-09-28” 2.2. “date_1[lt]” with a value of “2021-10-28”

  3. This is because in the URI parameter definition everything before the “=” sign is the parameter name and everything after is the parameter value.

  4. You can just do

String dateAsString = request.getParam("date1[gt]");
paramAsInteger = toDate(dateAsString)
  1. To implement the toDate() function read this simple article how to convert a string object into a data object using a standard library (link)
Advertisement