# Confusion between JodaCompatibleZonedDateTime and ZonedDateTime

**URL:** <https://discuss.elastic.co/t/confusion-between-jodacompatiblezoneddatetime-and-zoneddatetime/218384>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 7, 2020, 4:48pm UTC](https://discuss.elastic.co/t/confusion-between-jodacompatiblezoneddatetime-and-zoneddatetime/218384 "2020-02-07T16:48:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luflow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luflow/32/62300_2.png) [@luflow](https://discuss.elastic.co/u/luflow)\
**Post date:** [February 7, 2020, 4:48pm UTC](https://discuss.elastic.co/t/confusion-between-jodacompatiblezoneddatetime-and-zoneddatetime/218384/1 "2020-02-07T16:48:16Z")

</div>

Hi everyone!

I just tried to do some date calculatoin in painless and just don't get how I can access the functions of the JodaCompatibleZonedDateTime given by an index field.

In my index I have a field called "OXACTIVEFROM" with the following mapping:

```
"OXACTIVEFROM": {
  "type": "date",
  "format": "yyyy-MM-dd HH:mm:ss",
  "fields": {
    "unanalyzed": {
      "format": "yyyy-MM-dd HH:mm:ss",
      "type": "date"
    },
    "lowercase": {
      "format": "yyyy-MM-dd HH:mm:ss",
      "type": "date"
    },
    "sorting": {
      "format": "yyyy-MM-dd HH:mm:ss",
      "type": "date"
    }
  }
},

```

In an aggregation I use the following script, to calculate a boolean if this date is older than 60 days:

```
{
  "query": {...}
  "aggregations": {
    "60days": {
      "terms": {
        "script": {
          "params": {
            "now": 1581083160
          },
          "lang": "painless",
          "source": "if(!doc.containsKey('OXACTIVEFROM.unanalyzed') || doc['OXACTIVEFROM.unanalyzed'].size()==0 || doc['OXACTIVEFROM.unanalyzed'].value.getYear() == 0) return false; Instant instant = Instant.ofEpochMilli(params['now']); ZonedDateTime today = ZonedDateTime.ofInstant(instant, ZoneId.of('Z')); ZonedDateTime activefrom = doc['OXACTIVEFROM.unanalyzed'].value; return ChronoUnit.DAYS.between(activefrom, today) <= 60;"

        },
        "exclude": "false"
      },
    },
  }
}

```

This script just fails with

```
"type": "class_cast_exception",
"reason": "Cannot cast org.elasticsearch.script.JodaCompatibleZonedDateTime to java.time.ZonedDateTime""

```

For better readability the script again:

```
if(!doc.containsKey('OXACTIVEFROM.unanalyzed') || doc['OXACTIVEFROM.unanalyzed'].size()==0 || doc['OXACTIVEFROM.unanalyzed'].value.getYear() == 0) return false;
Instant instant = Instant.ofEpochMilli(params['now']);
ZonedDateTime today = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
ZonedDateTime activefrom = doc['OXACTIVEFROM.unanalyzed'].value;
return ChronoUnit.DAYS.between(activefrom, today) <= 60;

```

So it fails on the line where I get the value from the document and assign it to "activefrom"

So I googled and found the source of this class in ES: [https://github.com/elastic/elasticsearch/blob/7.3/server/src/main/java/org/elasticsearch/script/JodaCompatibleZonedDateTime.java#L75](https://github.com/elastic/elasticsearch/blob/7.3/server/src/main/java/org/elasticsearch/script/JodaCompatibleZonedDateTime.java#L75)

So I tried to use the "getZonedDateTime" to get the real ZoneDateTime:

```
ZonedDateTime activefrom = doc['OXACTIVEFROM.unanalyzed'].value.getZonedDateTime();
return ChronoUnit.DAYS.between(activefrom, today) <= 60;

```

But unfortunately this fails with:

```
"type": "illegal_argument_exception",
"reason": "dynamic method [org.elasticsearch.script.JodaCompatibleZonedDateTime, getZonedDateTime/0] not found"

```

Am I holding it wrong?!

Hopefully someone can enlighten me on this 😃

---

<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:** [March 6, 2020, 4:48pm UTC](https://discuss.elastic.co/t/confusion-between-jodacompatiblezoneddatetime-and-zoneddatetime/218384/2 "2020-03-06T16:48:35Z")

</div>

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