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 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");

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


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
    .format("yyyy-MM-dd HH:mm:ss");


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


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