Get current year from painless

Hi everyone!

In my application, I'm trying to retrieve the current year in a scripted field.
The idea was (in pseudocode):

calculated = currentYear - doc['field'].value

return calculated;

I tried with:

Instant Currentdate = Instant.ofEpochMilli(new Date().getTime());

And actually retrieves the current timestamp, but i can't access the Year value.

Is it possible, and if yes how, to get the current year value in Painless as an integer number?

Thank you for your time and patience!

Assuming your date field is called field and is mapped as date, this should do the trick:

String datetime = doc['field'].value.toString();
ZonedDateTime zdt = ZonedDateTime.parse(datetime);
emit(zdt.getYear());

Thank you :slight_smile: This actually worked :slight_smile:

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