Date not converting to different timezone in painless

Hi there,
I have date field in UTC and want to convert that date in to a different timezone and display it.
I created a new runtime field and made the type as date, but when I convert the date and emit it, the displayed date is the original one and not according to timezone. Below is the script

String date = '2022-04-27T01:39:29.030+00:00';
ZonedDateTime time1 = ZonedDateTime.parse(date);
Instant instant = time1.toInstant();
ZonedDateTime time2 = instant.atZone(ZoneId.of('America/Chicago')) ;
long a = time2.toInstant().toEpochMilli();
emit(a);

The output I'm getting is the same date i.e 2022-04-27T01:39:29.030+00:00 but it should be something like this 2022-04-26T20:39:29.030-05:00[America/Chicago] according to timezone.

I'm not able to figure out what I'm doing wrong here.

Hi @shivendra95

I did a quick test with your script. I had successful.
Maybe the problem is something else.

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "description": "",
          "lang": "painless",
          "source": """
              String date = '2022-04-27T01:39:29.030+00:00';
              ZonedDateTime time1 = ZonedDateTime.parse(date);
              Instant instant = time1.toInstant();
              ZonedDateTime time2 = instant.atZone(ZoneId.of('America/Chicago')) ;
              ctx.origin_date = time1;
              ctx.new_date = time2;
          """,
          "params": {
            "delimiter": "-",
            "position": 1
          }
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "new_date":""
      }
    }
  ]
}

Response:

        "_source" : {
          "origin_date" : "2022-04-27T01:39:29.030Z",
          "new_date" : "2022-04-26T20:39:29.030-05:00"
        },

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