# ZonedDateTime.parse function not working for custom DateTimeFormatter

**URL:** <https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819>\
**Category:** Kibana\
**Created:** [March 30, 2021, 4:25pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819 "2021-03-30T16:25:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [March 30, 2021, 4:25pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/1 "2021-03-30T16:25:23Z")

</div>

Below string to date time code is not working for me and it is returning empty array.  
script1:

> String datetime = '2021-03-25 08:41:22.011';  
> DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");  
> ZonedDateTime zdt = ZonedDateTime.parse(datetime, dtf);  
> return zdt;

when I changed the date format as below , the script is returning value.  
script2:

> String datetime = '2021-03-25 08:41:22.011 Z';  
> DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS VV");  
> ZonedDateTime zdt = ZonedDateTime.parse(datetime, dtf);  
> return zdt;

I have to parse the time with format "yyyy-MM-dd HH:mm:ss.SSS" into a date time object . Any idea why the script1 is not working. I have tried many formatting options but none is working.

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 30, 2021, 10:29pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/2 "2021-03-30T22:29:15Z")

</div>

Are you manually entering values to `String datetime` ? Or this is just an example?  
If not, where are the values coming from? Have samples of your raw data?  
Cheers!

---

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [March 31, 2021, 3:02am UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/3 "2021-03-31T03:02:57Z")

</div>

this is just an example. the values are coming from field doc[rtd.PARSER\_TIME] of keyword data type. tried below script but empty array returned without any error.

> String datetime = doc['rtd.PARSER\_TIME'].value;  
> DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");  
> ZonedDateTime zdt = ZonedDateTime.parse(datetime, dtf);  
> return zdt

Thanks for the support..

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 31, 2021, 4:52am UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/4 "2021-03-31T04:52:47Z")

</div>

Can you please show what you get when your script is like:

```auto
return doc['rtd.PARSER_TIME'].value

```

Some sample values ?!

---

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [March 31, 2021, 2:39pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/5 "2021-03-31T14:39:44Z")

</div>

it is returning document values as below.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/d/fde1f8e9d0dff6c9a4c28c67216551f1230e4274.png)

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [March 31, 2021, 10:33pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/6 "2021-03-31T22:33:35Z")

</div>

to troubleshoot your issue, try

```auto
ZonedDateTime zdt = ZonedDateTime.parse(datetime, DateTimeFormatter.RFC_1123_DATE_TIME);

```

I suspect that your dtf object is wrong?

---

<div class="post-metadata">

**Author:** ![rahulkothanath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rahulkothanath/32/99122_2.png) [@rahulkothanath](https://discuss.elastic.co/u/rahulkothanath)\
**Post date:** [April 1, 2021, 1:36pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/7 "2021-04-01T13:36:04Z")

</div>

it is still returning empty array. what I observed is your input string data must have a 'Z' at the end otherwise it is returning empty array as result.

Script below is took from Elasticsearch website and if you remove the 'Z' from the string and 'VV' from custom format it is returning empty array.

> String datetime = 'custom y 1983 m 10 d 13 22:15:30 Z';  
> DateTimeFormatter dtf = DateTimeFormatter.ofPattern(  
> "'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV");  
> ZonedDateTime zdt = ZonedDateTime.parse(datetime, dtf);

Thanks  
RK

---

<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:** [April 29, 2021, 1:36pm UTC](https://discuss.elastic.co/t/zoneddatetime-parse-function-not-working-for-custom-datetimeformatter/268819/8 "2021-04-29T13:36:19Z")

</div>

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