Invalid format: "2014-05-27T00:00:00.000Z" is malformed at "T00:00:00.000Z"

The following exception i am gettting with the below code snippet.The time which i have based is date object.but the milliseconds added default in the query.how can i overridden by java API.I am using 2.1 elasticsearch Version

RangeQueryBuilder dateRangeFilter = QueryBuilders.rangeQuery("first_occurance").timeZone("yyyy-MM-dd HH:mm:ss");
dateRangeFilter.gte(to.getTime());
dateRangeFilter.gte(from.getTime());

java.lang.IllegalArgumentException: Invalid format: "2014-05-27T00:00:00.000Z" is malformed at "T00:00:00.000Z"

Thanks
Moni

I think you mix a few things up. First of all, you provide a date format to the timeZone() method which produces yet another exception. Next, you are calling the getTime() on a date, which just outputs a numeric timestamp in milliseconds since epoch. I assume you previously just provided the Date object directly, which prints itself in ISO-8601 timestamp (which you see in your error message). In addition, you call the gte() method twice. I assume you want to call gte() for the from date and lte() for the to date.

This fully working example should get you started:

String format = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat df = new SimpleDateFormat(format);
// just create some random dates for this example...
Date from = df.parse("2014-01-01 10:05:12");
Date to = df.parse("2016-02-01 11:03:23");

RangeQueryBuilder dateRangeFilter = QueryBuilders
    .rangeQuery("first_occurance")
    .format("yyyy-MM-dd HH:mm:ss");
dateRangeFilter.gte(df.format(from));
dateRangeFilter.lte(df.format(to));

System.out.println(dateRangeFilter.toString());

This prints:

{
  "range" : {
    "first_occurance" : {
      "from" : "2014-01-01 10:05:12",
      "to" : "2016-02-01 11:03:23",
      "include_lower" : true,
      "include_upper" : true,
      "format" : "yyyy-MM-dd HH:mm:ss",
      "boost" : 1.0
    }
  }
}

Daniel

1 Like

Thanks for a quick response Daniel,It's Working fine.You are awesome.

1 Like

You're welcome. Glad I could help and your problem is now solved. :slight_smile:

1 Like