# Applying timezone for painless based time aggregation

**URL:** <https://discuss.elastic.co/t/applying-timezone-for-painless-based-time-aggregation/136785>\
**Category:** Elasticsearch\
**Created:** [June 21, 2018, 5:26am UTC](https://discuss.elastic.co/t/applying-timezone-for-painless-based-time-aggregation/136785 "2018-06-21T05:26:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_G](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anil_g/32/6645_2.png) [@Anil\_G](https://discuss.elastic.co/u/Anil_G)\
**Post date:** [June 21, 2018, 5:26am UTC](https://discuss.elastic.co/t/applying-timezone-for-painless-based-time-aggregation/136785/1 "2018-06-21T05:26:08Z")

</div>

I want to apply non standard time interval aggregation (hourOfDay, dayOfWeek etc.) on my date field.

```
{
   "size":0,
   "aggregations":{
      "groupby_timeinterval":{
         "terms":{
            "script":{
               "source":"def value = doc.start_time.value; return value.hourOfDay",
               "lang":"painless"
            }
         }
      }
   }
}

```

In ES 2.x, this above term aggregation used to allow "time\_zone" parameter that helped us get result aggregates also considering time\_zone but that support is no more from ES 5.x.

How can we apply timezone to the above query?  
Painless documentation doesn't given out any clues. When I tried to improvise and apply functions on the "value" (of type org.joda.time.MutableDateTime) I am getting "Unable to find dynamic method" error.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [June 22, 2018, 6:29pm UTC](https://discuss.elastic.co/t/applying-timezone-for-painless-based-time-aggregation/136785/2 "2018-06-22T18:29:14Z")

</div>

Can you be more specific in the error you get? It is likely the method you are trying to invoke is not whitelisted. Currently, doc values for date fields are exposed as joda time's ReadableDateTime, but the methods allowed are limited. Instead, we are slowly moving towards using the java time api. You can convert to using java time like this:

```auto
Instant mydate = Instant.ofEpochMilli(doc.start_time.value.millis);

```

Then you can convert to your time zone with `mydate.atZone(someZoneId)`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 20, 2018, 6:29pm UTC](https://discuss.elastic.co/t/applying-timezone-for-painless-based-time-aggregation/136785/3 "2018-07-20T18:29:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
