Home > Blockchain >  Elasticsearch equivalent date pattern for java ISO_LOCAL_DATE_TIME
Elasticsearch equivalent date pattern for java ISO_LOCAL_DATE_TIME

Time:08-05

In my pojo there is a LocalDateTime field which is formated with

DateTimeFormatter.ISO_LOCAL_DATE_TIME

I have to save the pojo in elasticsearch. What should be the relevant string pattern of this field in elasticsearch? I tried the following

"action_time": {
    "type": "date",
    "format": "yyyy-MM-dd hh:mm:sss"
}

When I print the field with ISO_LOCAL_DATE_TIME format in java I get this output

2022-08-04T16:29:09.866

What should be the proper pattern here?

CodePudding user response:

The "format" pattern in your JSON definition is not matching the one of ISO_LOCAL_DATE_TIME because it…

  • does not include a T between date and time of day and
  • contains 3 s, which would (possibly inpredictably) produce a three-digit value for seconds of minute, though those are limited to 59
  • does not contain any symbol that outputs a fraction of second (should be S)
  • uses lower-case symbols for hour of day, which is for 12-hour format and would require AM or PM somewhere in the output in order to be precise

Use this pattern instead:

"action_time": {
    "type": "date",
    "format": "yyyy-MM-dd'T'HH:mm:ss.SSS"
}

You can read more about those pattern symbols in the JavaDocs of DateTimeFormatter (here Java 9).

  • Related