# Elasticsearch inserting date type documents as UTC timezone datetime while indexing

**URL:** <https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197>\
**Category:** Elasticsearch\
**Created:** [August 11, 2014, 2:20pm UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197 "2014-08-11T14:20:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [August 11, 2014, 2:20pm UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/1 "2014-08-11T14:20:59Z")

</div>

Hi,

I'm using below code to insert some documents in elasticsearch index. But  
when inserting in the es the time is coming as UTC format rather than  
original GMT+5:30Z format. Where as the Sysout before indexing is giving me  
correct format as _2014-08-11T18:23:13.447+05:30_ . Please let me know how  
to keep the orizinal time format.

public static IndexResponse addDocumentsQuota(Client client, String index,  
String type, String categoryName) {

IndexResponse indexResponse = null;  
DateTime date = new DateTime();  
System.out.println(date);  
try {  
indexResponse = client.prepareIndex(index, type)  
.setSource(jsonBuilder()  
.startObject()  
.field("@timestamp", date)  
.field("category\_name", categoryName)  
.field("alert\_message", "alert message")  
.field("creation\_time", date)  
.endObject())  
.execute()  
.actionGet();  
} catch (ElasticsearchException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
} catch (IOException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
return indexResponse;  
}

_After indexing:_

"\_source": {  
"@timestamp": "2014-08-11T11:57:59.839Z",  
"category\_name": "newCat1",  
"alert\_message": "alert message",  
"creation\_time": "2014-08-11T11:57:59.852Z"  
}

Thanks,  
Subhadip

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5c82c9ab-a627-46a9-ad18-dda334524ab1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c82c9ab-a627-46a9-ad18-dda334524ab1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [August 12, 2014, 4:40am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/2 "2014-08-12T04:40:28Z")

</div>

Hi,

Any ideas how to prevent the time changing while indexing in es, or to  
convert in correct format while query ?

Thanks,  
Subhadip

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/33363b16-b2df-4bef-aded-db198c13d5ce%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/33363b16-b2df-4bef-aded-db198c13d5ce%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [August 12, 2014, 3:01pm UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/3 "2014-08-12T15:01:28Z")

</div>

Hi,

Can someone please give me a hint, I'm having trouble getting a solution  
for this.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d42cb94b-681d-4bd3-bf21-d955cd0af729%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d42cb94b-681d-4bd3-bf21-d955cd0af729%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [January 5, 2015, 4:42am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/4 "2015-01-05T04:42:11Z")

</div>

Hi,

Please help with suggestion.

On Monday, August 11, 2014 7:51:00 PM UTC+5:30, Subhadip Bagui wrote:

> Hi,
> 
> I'm using below code to insert some documents in elasticsearch index. But  
> when inserting in the es the time is coming as UTC format rather than  
> original GMT+5:30Z format. Where as the Sysout before indexing is giving me  
> correct format as _2014-08-11T18:23:13.447+05:30_ . Please let me know  
> how to keep the orizinal time format.
> 
> public static IndexResponse addDocumentsQuota(Client client, String index,  
> String type, String categoryName) {
> 
> IndexResponse indexResponse = null;  
> DateTime date = new DateTime();  
> System.out.println(date);  
> try {  
> indexResponse = client.prepareIndex(index, type)  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("@timestamp", date)  
> .field("category\_name", categoryName)  
> .field("alert\_message", "alert message")  
> .field("creation\_time", date)  
> .endObject())  
> .execute()  
> .actionGet();  
> } catch (ElasticsearchException e) {  
> // TODO Auto-generated catch block  
> e.printStackTrace();  
> } catch (IOException e) {  
> // TODO Auto-generated catch block  
> e.printStackTrace();  
> }  
> return indexResponse;  
> }
> 
> _After indexing:_
> 
> "\_source": {  
> "@timestamp": "2014-08-11T11:57:59.839Z",  
> "category\_name": "newCat1",  
> "alert\_message": "alert message",  
> "creation\_time": "2014-08-11T11:57:59.852Z"  
> }
> 
> Thanks,  
> Subhadip

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a0a623d6-524c-4928-a35d-d042855c1834%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a0a623d6-524c-4928-a35d-d042855c1834%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [January 5, 2015, 4:45am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/5 "2015-01-05T04:45:15Z")

</div>

Hi,

Please help with suggestions.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/02d96b0a-99a7-43f3-b245-13418dece674%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/02d96b0a-99a7-43f3-b245-13418dece674%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![sumit\_gupta\_sgt](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@sumit\_gupta\_sgt](https://discuss.elastic.co/u/sumit_gupta_sgt)\
**Post date:** [January 5, 2015, 4:55am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/6 "2015-01-05T04:55:46Z")

</div>

Try to insert using Milliseconds.

Thanks,  
Sumit

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [January 6, 2015, 6:51am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/7 "2015-01-06T06:51:08Z")

</div>

HI,

XContentBuilder (I assume jsonBuilder() returns it) serialises date using UTC timezone by default.  
If you’d like to use different format, you’d need to build your own DateTimeFormatter and pass it when you add date type field.

## For example, to use ISO date time format with specified timezone:

## DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(ZONE\_NAME))); : .field("@timestamp", date, formatter) : .field("creation\_time", date, formatter) :

Masaru

On January 5, 2015 at 13:45:19, Subhadip Bagui ([i.bagui@gmail.com](mailto:i.bagui@gmail.com)) wrote:

> Hi,
> 
> Please help with suggestions.
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch"  
> group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/02d96b0a-99a7-43f3-b245-13418dece674%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/02d96b0a-99a7-43f3-b245-13418dece674%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.54ab8619.6b8b4567.13b%40citra.local](https://groups.google.com/d/msgid/elasticsearch/etPan.54ab8619.6b8b4567.13b%40citra.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [January 7, 2015, 11:12am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/8 "2015-01-07T11:12:54Z")

</div>

Hi,

I tried with what you suggested using below code. But getting exception.

DateTime dateTime = new org.joda.time.DateTime();  
System.out.println(dateTime);  
DateTimeFormatter dateTimeFormatter =  
ISODateTimeFormat.dateTime().withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Calcutta")));

try {  
response = client  
.prepareIndex("node-test", "node-entry")  
.setSource(XContentFactory.jsonBuilder()  
.startObject()  
.field("NodeName", nodeName)  
.field("NodeStatus", nodeStatus)  
.field("@timestamp", dateTime, dateTimeFormatter)  
.endObject())  
.execute().actionGet();  
} catch (ElasticsearchException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}

org.elasticsearch.transport.TransportSerializationException: Failed to  
deserialize exception response from stream  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:169)  
at  
org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:123)  
at  
org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)  
at  
org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)  
at  
org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)  
at  
org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:296)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eab0ae36-cd4e-42cc-bf8b-8e59ed24e678%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eab0ae36-cd4e-42cc-bf8b-8e59ed24e678%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [January 8, 2015, 6:21am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/9 "2015-01-08T06:21:49Z")

</div>

Hi,

Not sure what’s failing because of deserialize issue. It usually happens when you use different version of java.  
You can see what the problem is by checking ES log instead too.

## To check you are sending document in correct format, you may want to print JSON before sending index request:

XContentBuilder builder = XContentFactory.jsonBuilder().startObject()  
:  
.field("@timestamp", dateTime, dateTimeFormatter)  
.endObject();

System.out.println(builder.string());

## try{ client.prepareIndex(“node-test”, “node-entry”).setSource(builder).execute().actionGet(); } catch (Exception e) { e.printStackTrace(); }

Masaru

On January 7, 2015 at 20:12:59, Subhadip Bagui ([i.bagui@gmail.com](mailto:i.bagui@gmail.com)) wrote:

> Hi,
> 
> I tried with what you suggested using below code. But getting exception.
> 
> DateTime dateTime = new org.joda.time.DateTime();  
> System.out.println(dateTime);  
> DateTimeFormatter dateTimeFormatter =  
> ISODateTimeFormat.dateTime().withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Calcutta")));
> 
> try {  
> response = client  
> .prepareIndex("node-test", "node-entry")  
> .setSource(XContentFactory.jsonBuilder()  
> .startObject()  
> .field("NodeName", nodeName)  
> .field("NodeStatus", nodeStatus)  
> .field("@timestamp", dateTime, dateTimeFormatter)  
> .endObject())  
> .execute().actionGet();  
> } catch (ElasticsearchException e) {  
> // TODO Auto-generated catch block  
> e.printStackTrace();  
> }
> 
> org.elasticsearch.transport.TransportSerializationException: Failed to  
> deserialize exception response from stream  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:169)  
> at  
> org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:123)  
> at  
> org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70)  
> at  
> org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564)  
> at  
> org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)  
> at  
> org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:296)
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch"  
> group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/eab0ae36-cd4e-42cc-bf8b-8e59ed24e678%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/eab0ae36-cd4e-42cc-bf8b-8e59ed24e678%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.54ae2239.74b0dc51.677%40citra.local](https://groups.google.com/d/msgid/elasticsearch/etPan.54ae2239.74b0dc51.677%40citra.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:40am UTC](https://discuss.elastic.co/t/elasticsearch-inserting-date-type-documents-as-utc-timezone-datetime-while-indexing/19197/10 "2017-07-06T00:40:11Z")

</div>


