# Java API to create index with date

**URL:** <https://discuss.elastic.co/t/java-api-to-create-index-with-date/53044>\
**Category:** Elasticsearch\
**Created:** [June 16, 2016, 7:07pm UTC](https://discuss.elastic.co/t/java-api-to-create-index-with-date/53044 "2016-06-16T19:07:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zfs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zfs/32/13370_2.png) [@Zfs](https://discuss.elastic.co/u/Zfs)\
**Post date:** [June 16, 2016, 7:07pm UTC](https://discuss.elastic.co/t/java-api-to-create-index-with-date/53044/1 "2016-06-16T19:07:22Z")

</div>

If I want to have my index rotated daily, so I would need to pass the date as part of the index name? Is there an option for that or do I have to append it to the name manually?  
I'm using the example below from the Java docs, is it possible to print out the json Object as a string for debugging purposes?

```
				IndexResponse response = client.prepareIndex("test_db", "metrics")
				        .setSource(jsonBuilder()
				                    .startObject()
				                        .field("user", "kimchy")
				                        .field("postDate", new Date())
				                        .field("message", "trying out Elasticsearch")
				                        .field("number", 10)
				                    .endObject()
				                  )
				        .get();

```

thanks

---

<div class="post-metadata">

**Author:** ![Zfs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zfs/32/13370_2.png) [@Zfs](https://discuss.elastic.co/u/Zfs)\
**Post date:** [June 16, 2016, 7:40pm UTC](https://discuss.elastic.co/t/java-api-to-create-index-with-date/53044/2 "2016-06-16T19:40:08Z")

</div>

I added this:

```
    	DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
    	Date today = Calendar.getInstance().getTime();
    	String indexDate = df.format(today);

        IndexResponse response = client.prepareIndex("test_db-"+indexDate, "metrics")
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 17, 2016, 7:11am UTC](https://discuss.elastic.co/t/java-api-to-create-index-with-date/53044/3 "2016-06-17T07:11:55Z")

</div>

Have you checked the [date math support in index names](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html) documentation? I think that might help you...

--Alex

---

<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 5, 2017, 10:42pm UTC](https://discuss.elastic.co/t/java-api-to-create-index-with-date/53044/4 "2017-07-05T22:42:55Z")

</div>


