2019-06-03 16:22:45 -04:00
|
|
|
[[painless-datetime]]
|
|
|
|
=== Using Datetime in Painless
|
|
|
|
|
|
|
|
==== Datetime API
|
|
|
|
|
|
|
|
Datetimes in Painless use the standard Java libraries and are available through
|
|
|
|
the Painless <<painless-api-reference-shared, Shared API>>. Most of the classes
|
|
|
|
from the following Java packages are available to use in Painless scripts:
|
|
|
|
|
|
|
|
* <<painless-api-reference-shared-java-time, java.time>>
|
|
|
|
* <<painless-api-reference-shared-java-time-chrono, java.time.chrono>>
|
|
|
|
* <<painless-api-reference-shared-java-time-format, java.time.format>>
|
|
|
|
* <<painless-api-reference-shared-java-time-temporal, java.time.temporal>>
|
|
|
|
* <<painless-api-reference-shared-java-time-zone, java.time.zone>>
|
|
|
|
|
|
|
|
==== Datetime Representation
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Datetimes in Painless are most commonly represented as a numeric value, a
|
|
|
|
string value, or a complex value.
|
|
|
|
|
|
|
|
numeric:: a datetime representation as a number from a starting offset called
|
|
|
|
an epoch; in Painless this is typically a <<primitive-types, long>> as
|
|
|
|
milliseconds since an epoch of 1970-01-01 00:00:00 Zulu Time
|
|
|
|
string:: a datetime representation as a sequence of characters defined by
|
|
|
|
a standard format or a custom format; in Painless this is typically a
|
|
|
|
<<string-type, String>> of the standard format
|
|
|
|
https://en.wikipedia.org/wiki/ISO_8601[ISO 8601]
|
|
|
|
complex:: a datetime representation as a complex type
|
|
|
|
(<<reference-types, object>>) that abstracts away internal details of how the
|
|
|
|
datetime is stored and often provides utilities for modification and
|
|
|
|
comparison; in Painless this is typically a
|
2019-07-02 00:29:01 -04:00
|
|
|
<<painless-api-reference-shared-ZonedDateTime, ZonedDateTime>>
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
Switching between different representations of datetimes is often necessary to
|
|
|
|
achieve a script's objective(s). A typical pattern in a script is to switch a
|
2019-06-05 13:20:46 -04:00
|
|
|
numeric or string datetime to a complex datetime, modify or compare the complex
|
|
|
|
datetime, and then switch it back to a numeric or string datetime for storage
|
|
|
|
or to return a result.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
==== Datetime Parsing and Formatting
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Datetime parsing is a switch from a string datetime to a complex datetime, and
|
|
|
|
datetime formatting is a switch from a complex datetime to a string datetime.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
A <<painless-api-reference-shared-DateTimeFormatter, DateTimeFormatter>> is a
|
2019-06-05 13:20:46 -04:00
|
|
|
complex type (<<reference-types, object>>) that defines the allowed sequence
|
|
|
|
of characters for a string datetime. Datetime parsing and formatting often
|
2020-01-02 13:13:00 -05:00
|
|
|
require a DateTimeFormatter. For more information about how to use a
|
2019-06-05 13:20:46 -04:00
|
|
|
DateTimeFormatter see the
|
2019-06-03 16:22:45 -04:00
|
|
|
{java11-javadoc}/java.base/java/time/format/DateTimeFormatter.html[Java documentation].
|
|
|
|
|
|
|
|
===== Datetime Parsing Examples
|
|
|
|
|
|
|
|
* parse from milliseconds
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String milliSinceEpochString = "434931330000";
|
|
|
|
long milliSinceEpoch = Long.parseLong(milliSinceEpochString);
|
|
|
|
Instant instant = Instant.ofEpochMilli(milliSinceEpoch);
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
|
|
|
|
----
|
|
|
|
+
|
|
|
|
* parse from ISO 8601
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String datetime = '1983-10-13T22:15:30Z';
|
2019-06-17 13:58:53 -04:00
|
|
|
ZonedDateTime zdt = ZonedDateTime.parse(datetime); <1>
|
2019-06-03 16:22:45 -04:00
|
|
|
----
|
2019-06-17 13:58:53 -04:00
|
|
|
<1> Note the parse method uses ISO 8601 by default.
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
* parse from RFC 1123
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String datetime = 'Thu, 13 Oct 1983 22:15:30 GMT';
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.parse(datetime,
|
2019-06-17 13:58:53 -04:00
|
|
|
DateTimeFormatter.RFC_1123_DATE_TIME); <1>
|
2019-06-03 16:22:45 -04:00
|
|
|
----
|
2019-06-17 13:58:53 -04:00
|
|
|
<1> Note the use of a built-in DateTimeFormatter.
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
* parse from a custom format
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String datetime = 'custom y 1983 m 10 d 13 22:15:30 Z';
|
|
|
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
|
|
|
|
"'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV");
|
2019-06-17 13:58:53 -04:00
|
|
|
ZonedDateTime zdt = ZonedDateTime.parse(datetime, dtf); <1>
|
2019-06-03 16:22:45 -04:00
|
|
|
----
|
2019-06-17 13:58:53 -04:00
|
|
|
<1> Note the use of a custom DateTimeFormatter.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
===== Datetime Formatting Examples
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
* format to ISO 8601
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
2019-06-17 13:58:53 -04:00
|
|
|
String datetime = zdt.format(DateTimeFormatter.ISO_INSTANT); <1>
|
2019-06-03 16:22:45 -04:00
|
|
|
----
|
2019-06-17 13:58:53 -04:00
|
|
|
<1> Note the use of a built-in DateTimeFormatter.
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* format to a custom format
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
|
|
|
|
"'date:' yyyy/MM/dd 'time:' HH:mm:ss");
|
2019-06-17 13:58:53 -04:00
|
|
|
String datetime = zdt.format(dtf); <1>
|
2019-06-03 16:22:45 -04:00
|
|
|
----
|
2019-06-17 13:58:53 -04:00
|
|
|
<1> Note the use of a custom DateTimeFormatter.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
==== Datetime Conversion
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Datetime conversion is a switch from a numeric datetime to a complex datetime
|
|
|
|
and vice versa.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
===== Datetime Conversion Examples
|
|
|
|
|
|
|
|
* convert from milliseconds
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
long milliSinceEpoch = 434931330000L;
|
|
|
|
Instant instant = Instant.ofEpochMilli(milliSinceEpoch);
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
|
|
|
|
----
|
|
|
|
+
|
|
|
|
* convert to milliseconds
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
-----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
long milliSinceEpoch = zdt.toInstant().toEpochMilli();
|
|
|
|
-----
|
|
|
|
|
|
|
|
==== Datetime Pieces
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Datetime representations often contain the data to extract individual datetime
|
|
|
|
pieces such as year, hour, timezone, etc. Use individual pieces of a datetime
|
|
|
|
to create a complex datetime, and use a complex datetime to extract individual
|
|
|
|
pieces.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
===== Datetime Pieces Examples
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
* create a complex datetime from pieces
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
int year = 1983;
|
|
|
|
int month = 10;
|
|
|
|
int day = 13;
|
|
|
|
int hour = 22;
|
|
|
|
int minutes = 15;
|
|
|
|
int seconds = 30;
|
|
|
|
int nanos = 0;
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.of(
|
|
|
|
year, month, day, hour, minutes, seconds, nanos, ZoneId.of('Z'));
|
|
|
|
----
|
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* extract pieces from a complex datetime
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 100, ZoneId.of(tz));
|
|
|
|
int year = zdt.getYear();
|
|
|
|
int month = zdt.getMonthValue();
|
|
|
|
int day = zdt.getDayOfMonth();
|
|
|
|
int hour = zdt.getHour();
|
|
|
|
int minutes = zdt.getMinute();
|
|
|
|
int seconds = zdt.getSecond();
|
|
|
|
int nanos = zdt.getNano();
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Datetime Modification
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Use either a numeric datetime or a complex datetime to do modification such as
|
|
|
|
adding several seconds to a datetime or subtracting several days from a
|
|
|
|
datetime. Use standard <<painless-operators-numeric, numeric operators>> to
|
|
|
|
modify a numeric datetime. Use
|
|
|
|
<<painless-api-reference-shared-ZonedDateTime, methods>> (or fields) to modify
|
|
|
|
a complex datetime. Note many complex datetimes are immutable so upon
|
|
|
|
modification a new complex datetime is created that requires
|
|
|
|
<<variable-assignment, assignment>> or immediate use.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
===== Datetime Modification Examples
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
* Subtract three seconds from a numeric datetime in milliseconds
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
long milliSinceEpoch = 434931330000L;
|
|
|
|
milliSinceEpoch = milliSinceEpoch - 1000L*3L;
|
|
|
|
----
|
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* Add three days to a complex datetime
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime updatedZdt = zdt.plusDays(3);
|
|
|
|
----
|
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* Subtract 125 minutes from a complex datetime
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime updatedZdt = zdt.minusMinutes(125);
|
|
|
|
----
|
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* Set the year on a complex datetime
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime updatedZdt = zdt.withYear(1976);
|
|
|
|
----
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
==== Datetime Difference (Elapsed Time)
|
2019-06-03 16:22:45 -04:00
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Use either two numeric datetimes or two complex datetimes to calculate the
|
|
|
|
difference (elapsed time) between two different datetimes. Use
|
2020-01-02 13:13:00 -05:00
|
|
|
<<subtraction-operator, subtraction>> to calculate the difference between two
|
|
|
|
numeric datetimes of the same time unit such as milliseconds. For
|
2019-06-05 13:20:46 -04:00
|
|
|
complex datetimes there is often a method or another complex type
|
|
|
|
(<<reference-types, object>>) available to calculate the difference. Use
|
|
|
|
<<painless-api-reference-shared-ChronoUnit, ChronoUnit>>
|
|
|
|
to calculate the difference between two complex datetimes if supported.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
2019-06-17 13:58:53 -04:00
|
|
|
===== Datetime Difference Examples
|
2019-06-03 16:22:45 -04:00
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
* Difference in milliseconds between two numeric datetimes
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
long startTimestamp = 434931327000L;
|
|
|
|
long endTimestamp = 434931330000L;
|
|
|
|
long differenceInMillis = endTimestamp - startTimestamp;
|
|
|
|
----
|
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* Difference in milliseconds between two complex datetimes
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt1 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime zdt2 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 35, 0, ZoneId.of('Z'));
|
|
|
|
long differenceInMillis = ChronoUnit.MILLIS.between(zdt1, zdt2);
|
|
|
|
----
|
|
|
|
+
|
2019-06-05 13:20:46 -04:00
|
|
|
* Difference in days between two complex datetimes
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt1 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime zdt2 =
|
|
|
|
ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));
|
|
|
|
long differenceInDays = ChronoUnit.DAYS.between(zdt1, zdt2);
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Datetime Comparison
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
Use either two numeric datetimes or two complex datetimes to do a datetime
|
|
|
|
comparison. Use standard <<painless-operators-boolean, comparison operators>>
|
|
|
|
to compare two numeric datetimes of the same time unit such as milliseconds.
|
|
|
|
For complex datetimes there is often a method or another complex type
|
|
|
|
(<<reference-types, object>>) available to do the comparison.
|
2019-06-03 16:22:45 -04:00
|
|
|
|
|
|
|
===== Datetime Comparison Examples
|
|
|
|
|
2019-06-05 13:20:46 -04:00
|
|
|
* Greater than comparison of two numeric datetimes in milliseconds
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
long timestamp1 = 434931327000L;
|
|
|
|
long timestamp2 = 434931330000L;
|
|
|
|
|
|
|
|
if (timestamp1 > timestamp2) {
|
|
|
|
// handle condition
|
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
2019-12-23 12:38:17 -05:00
|
|
|
* Equality comparison of two complex datetimes
|
2019-06-05 13:20:46 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt1 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime zdt2 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
|
|
|
|
if (zdt1.equals(zdt2)) {
|
|
|
|
// handle condition
|
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
2019-12-23 12:38:17 -05:00
|
|
|
* Less than comparison of two complex datetimes
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt1 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime zdt2 =
|
|
|
|
ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));
|
|
|
|
|
|
|
|
if (zdt1.isBefore(zdt2)) {
|
|
|
|
// handle condition
|
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
2019-12-23 12:38:17 -05:00
|
|
|
* Greater than comparison of two complex datetimes
|
2019-06-03 16:22:45 -04:00
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime zdt1 =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime zdt2 =
|
|
|
|
ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));
|
|
|
|
|
|
|
|
if (zdt1.isAfter(zdt2)) {
|
|
|
|
// handle condition
|
|
|
|
}
|
|
|
|
----
|
2019-06-17 13:58:53 -04:00
|
|
|
|
2019-07-02 00:29:01 -04:00
|
|
|
==== Datetime Zone
|
|
|
|
|
|
|
|
Both string datetimes and complex datetimes have a timezone with a default of
|
|
|
|
`UTC`. Numeric datetimes do not have enough explicit information to
|
|
|
|
have a timezone, so `UTC` is always assumed. Use
|
|
|
|
<<painless-api-reference-shared-ZonedDateTime, methods>> (or fields) in
|
|
|
|
conjunction with a <<painless-api-reference-shared-ZoneId, ZoneId>> to change
|
|
|
|
the timezone for a complex datetime. Parse a string datetime into a complex
|
|
|
|
datetime to change the timezone, and then format the complex datetime back into
|
|
|
|
a desired string datetime. Note many complex datetimes are immutable so upon
|
|
|
|
modification a new complex datetime is created that requires
|
|
|
|
<<variable-assignment, assignment>> or immediate use.
|
|
|
|
|
|
|
|
===== Datetime Zone Examples
|
|
|
|
|
|
|
|
* Modify the timezone for a complex datetime
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime utc =
|
|
|
|
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
|
|
|
|
ZonedDateTime pst = utc.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));
|
|
|
|
----
|
|
|
|
+
|
|
|
|
* Modify the timezone for a string datetime
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String gmtString = 'Thu, 13 Oct 1983 22:15:30 GMT';
|
|
|
|
ZonedDateTime gmtZdt = ZonedDateTime.parse(gmtString,
|
|
|
|
DateTimeFormatter.RFC_1123_DATE_TIME); <1>
|
|
|
|
ZonedDateTime pstZdt =
|
|
|
|
gmtZdt.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));
|
|
|
|
String pstString = pstZdt.format(DateTimeFormatter.RFC_1123_DATE_TIME);
|
|
|
|
----
|
|
|
|
<1> Note the use of a built-in DateTimeFormatter.
|
|
|
|
|
2019-06-17 13:58:53 -04:00
|
|
|
==== Datetime Input
|
|
|
|
|
|
|
|
There are several common ways datetimes are used as input for a script
|
|
|
|
determined by the <<painless-contexts, Painless context>>. Typically, datetime
|
|
|
|
input will be accessed from parameters specified by the user, from an original
|
|
|
|
source document, or from an indexed document.
|
|
|
|
|
|
|
|
===== Datetime Input From User Parameters
|
|
|
|
|
|
|
|
Use the {ref}/modules-scripting-using.html#_script_parameters[params section]
|
|
|
|
during script specification to pass in a numeric datetime or string datetime as
|
|
|
|
a script input. Access to user-defined parameters within a script is dependent
|
|
|
|
on the Painless context, though, the parameters are most commonly accessible
|
|
|
|
through an input called `params`.
|
|
|
|
|
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Parse a numeric datetime from user parameters to a complex datetime
|
|
|
|
+
|
|
|
|
** Input:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
...
|
|
|
|
"script": {
|
|
|
|
...
|
|
|
|
"params": {
|
|
|
|
"input_datetime": 434931327000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
2019-07-02 18:38:16 -04:00
|
|
|
long inputDateTime = params['input_datetime'];
|
2019-06-17 13:58:53 -04:00
|
|
|
Instant instant = Instant.ofEpochMilli(inputDateTime);
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
|
|
|
|
----
|
|
|
|
+
|
|
|
|
* Parse a string datetime from user parameters to a complex datetime
|
|
|
|
+
|
|
|
|
** Input:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
...
|
|
|
|
"script": {
|
|
|
|
...
|
|
|
|
"params": {
|
|
|
|
"input_datetime": "custom y 1983 m 10 d 13 22:15:30 Z"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String datetime = params['input_datetime'];
|
|
|
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
|
|
|
|
"'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV");
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.parse(datetime, dtf); <1>
|
|
|
|
----
|
|
|
|
<1> Note the use of a custom DateTimeFormatter.
|
|
|
|
|
|
|
|
===== Datetime Input From a Source Document
|
|
|
|
|
|
|
|
Use an original {ref}/mapping-source-field.html[source] document as a script
|
|
|
|
input to access a numeric datetime or string datetime for a specific field
|
|
|
|
within that document. Access to an original source document within a script is
|
|
|
|
dependent on the Painless context and is not always available. An original
|
|
|
|
source document is most commonly accessible through an input called
|
|
|
|
`ctx['_source']` or `params['_source']`.
|
|
|
|
|
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Parse a numeric datetime from a sourced document to a complex datetime
|
|
|
|
+
|
|
|
|
** Input:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
...
|
|
|
|
"input_datetime": 434931327000
|
|
|
|
...
|
2019-06-17 13:58:53 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
2019-07-02 18:38:16 -04:00
|
|
|
long inputDateTime = ctx['_source']['input_datetime']; <1>
|
2019-06-17 13:58:53 -04:00
|
|
|
Instant instant = Instant.ofEpochMilli(inputDateTime);
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));
|
|
|
|
----
|
|
|
|
<1> Note access to `_source` is dependent on the Painless context.
|
|
|
|
+
|
|
|
|
* Parse a string datetime from a sourced document to a complex datetime
|
|
|
|
+
|
|
|
|
** Input:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
...
|
|
|
|
"input_datetime": "1983-10-13T22:15:30Z"
|
|
|
|
...
|
2019-06-17 13:58:53 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String datetime = params['_source']['input_datetime']; <1>
|
|
|
|
ZonedDateTime zdt = ZonedDateTime.parse(datetime); <2>
|
|
|
|
----
|
|
|
|
<1> Note access to `_source` is dependent on the Painless context.
|
|
|
|
<2> Note the parse method uses ISO 8601 by default.
|
|
|
|
|
|
|
|
===== Datetime Input From an Indexed Document
|
|
|
|
|
|
|
|
Use an indexed document as a script input to access a complex datetime for a
|
|
|
|
specific field within that document where the field is mapped as a
|
|
|
|
{ref}/date.html[standard date] or a {ref}/date_nanos.html[nanosecond date].
|
|
|
|
Numeric datetime fields mapped as {ref}/number.html[numeric] and string
|
|
|
|
datetime fields mapped as {ref}/keyword.html[keyword] are accessible through an
|
|
|
|
indexed document as well. Access to an indexed document within a script is
|
|
|
|
dependent on the Painless context and is not always available. An indexed
|
|
|
|
document is most commonly accessible through an input called `doc`.
|
|
|
|
|
|
|
|
*Examples*
|
|
|
|
|
|
|
|
* Format a complex datetime from an indexed document to a string datetime
|
|
|
|
+
|
|
|
|
** Assumptions:
|
|
|
|
+
|
|
|
|
*** The field `input_datetime` exists in all indexes as part of the query
|
|
|
|
*** All indexed documents contain the field `input_datetime`
|
|
|
|
+
|
|
|
|
** Mappings:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"mappings": {
|
|
|
|
...
|
|
|
|
"properties": {
|
|
|
|
...
|
|
|
|
"input_datetime": {
|
|
|
|
"type": "date"
|
|
|
|
}
|
|
|
|
...
|
2019-06-17 13:58:53 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
...
|
|
|
|
}
|
2019-06-17 13:58:53 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
2019-07-31 11:21:46 -04:00
|
|
|
ZonedDateTime input = doc['input_datetime'].value;
|
2019-06-17 13:58:53 -04:00
|
|
|
String output = input.format(DateTimeFormatter.ISO_INSTANT); <1>
|
|
|
|
----
|
|
|
|
<1> Note the use of a built-in DateTimeFormatter.
|
|
|
|
+
|
|
|
|
* Find the difference between two complex datetimes from an indexed document
|
|
|
|
+
|
|
|
|
** Assumptions:
|
|
|
|
+
|
2019-07-02 00:29:01 -04:00
|
|
|
*** The fields `start` and `end` may *not* exist in all indexes as part of the
|
|
|
|
query
|
|
|
|
*** The fields `start` and `end` may *not* have values in all indexed documents
|
2019-06-17 13:58:53 -04:00
|
|
|
+
|
|
|
|
** Mappings:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"mappings": {
|
|
|
|
...
|
|
|
|
"properties": {
|
|
|
|
...
|
|
|
|
"start": {
|
|
|
|
"type": "date"
|
|
|
|
},
|
|
|
|
"end": {
|
|
|
|
"type": "date"
|
|
|
|
}
|
|
|
|
...
|
2019-06-17 13:58:53 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
...
|
|
|
|
}
|
2019-06-17 13:58:53 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
2019-07-02 00:29:01 -04:00
|
|
|
if (doc.containsKey('start') && doc.containsKey('end')) { <1>
|
2019-06-17 13:58:53 -04:00
|
|
|
|
2019-07-02 00:29:01 -04:00
|
|
|
if (doc['start'].size() > 0 && doc['end'].size() > 0) { <2>
|
2019-06-17 13:58:53 -04:00
|
|
|
|
2019-07-31 11:21:46 -04:00
|
|
|
ZonedDateTime start = doc['start'].value;
|
|
|
|
ZonedDateTime end = doc['end'].value;
|
2019-07-02 00:29:01 -04:00
|
|
|
long differenceInMillis = ChronoUnit.MILLIS.between(start, end);
|
2019-06-17 13:58:53 -04:00
|
|
|
|
|
|
|
// handle difference in times
|
|
|
|
} else {
|
|
|
|
// handle fields without values
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// handle index with missing fields
|
|
|
|
}
|
|
|
|
----
|
|
|
|
<1> When a query's results span multiple indexes, some indexes may not
|
|
|
|
contain a specific field. Use the `containsKey` method call on the `doc` input
|
|
|
|
to ensure a field exists as part of the index for the current document.
|
2019-07-02 18:38:16 -04:00
|
|
|
<2> Some fields within a document may have no values. Use the `size` method
|
2019-06-17 13:58:53 -04:00
|
|
|
call on a field within the `doc` input to ensure that field has at least one
|
|
|
|
value for the current document.
|
2019-07-02 18:38:16 -04:00
|
|
|
|
|
|
|
==== Datetime Now
|
|
|
|
|
|
|
|
Under most Painless contexts the current datetime, `now`, is not supported.
|
2020-01-02 13:13:00 -05:00
|
|
|
There are two primary reasons for this. The first is that scripts are often run once
|
2019-07-02 18:38:16 -04:00
|
|
|
per document, so each time the script is run a different `now` is returned. The
|
2020-01-02 13:13:00 -05:00
|
|
|
second is that scripts are often run in a distributed fashion without a way to
|
2019-07-02 18:38:16 -04:00
|
|
|
appropriately synchronize `now`. Instead, pass in a user-defined parameter with
|
|
|
|
either a string datetime or numeric datetime for `now`. A numeric datetime is
|
2019-12-23 12:38:17 -05:00
|
|
|
preferred as there is no need to parse it for comparison.
|
2019-07-02 18:38:16 -04:00
|
|
|
|
|
|
|
===== Datetime Now Examples
|
|
|
|
|
|
|
|
* Use a numeric datetime as `now`
|
|
|
|
+
|
|
|
|
** Assumptions:
|
|
|
|
+
|
|
|
|
*** The field `input_datetime` exists in all indexes as part of the query
|
|
|
|
*** All indexed documents contain the field `input_datetime`
|
|
|
|
+
|
|
|
|
** Mappings:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"mappings": {
|
|
|
|
...
|
|
|
|
"properties": {
|
2019-07-02 18:38:16 -04:00
|
|
|
...
|
2020-07-17 11:31:37 -04:00
|
|
|
"input_datetime": {
|
|
|
|
"type": "date"
|
|
|
|
}
|
|
|
|
...
|
2019-07-02 18:38:16 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
...
|
|
|
|
}
|
2019-07-02 18:38:16 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Input:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
...
|
|
|
|
"script": {
|
|
|
|
...
|
|
|
|
"params": {
|
|
|
|
"now": <generated numeric datetime in milliseconds since epoch>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
long now = params['now'];
|
2019-07-31 11:21:46 -04:00
|
|
|
ZonedDateTime inputDateTime = doc['input_datetime'];
|
2019-07-02 18:38:16 -04:00
|
|
|
long millisDateTime = zdt.toInstant().toEpochMilli();
|
|
|
|
long elapsedTime = now - millisDateTime;
|
|
|
|
----
|
|
|
|
+
|
|
|
|
* Use a string datetime as `now`
|
|
|
|
+
|
|
|
|
** Assumptions:
|
|
|
|
+
|
|
|
|
*** The field `input_datetime` exists in all indexes as part of the query
|
|
|
|
*** All indexed documents contain the field `input_datetime`
|
|
|
|
+
|
|
|
|
** Mappings:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"mappings": {
|
|
|
|
...
|
|
|
|
"properties": {
|
|
|
|
...
|
|
|
|
"input_datetime": {
|
|
|
|
"type": "date"
|
|
|
|
}
|
|
|
|
...
|
2019-07-02 18:38:16 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
...
|
|
|
|
}
|
2019-07-02 18:38:16 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Input:
|
|
|
|
+
|
|
|
|
[source,JSON]
|
|
|
|
----
|
|
|
|
...
|
|
|
|
"script": {
|
|
|
|
...
|
|
|
|
"params": {
|
|
|
|
"now": "<generated string datetime in ISO-8601>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
...
|
|
|
|
----
|
|
|
|
+
|
|
|
|
** Script:
|
|
|
|
+
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
String nowString = params['now'];
|
|
|
|
ZonedDateTime nowZdt = ZonedDateTime.parse(datetime); <1>
|
|
|
|
long now = ZonedDateTime.toInstant().toEpochMilli();
|
2019-07-31 11:21:46 -04:00
|
|
|
ZonedDateTime inputDateTime = doc['input_datetime'];
|
2019-07-02 18:38:16 -04:00
|
|
|
long millisDateTime = zdt.toInstant().toEpochMilli();
|
|
|
|
long elapsedTime = now - millisDateTime;
|
|
|
|
----
|
|
|
|
<1> Note this parses the same string datetime every time the script runs. Use a
|
|
|
|
numeric datetime to avoid a significant performance hit.
|
2019-07-31 11:21:46 -04:00
|
|
|
|
|
|
|
==== Datetime Examples in Contexts
|
|
|
|
|
|
|
|
===== Load the Example Data
|
|
|
|
|
|
|
|
Run the following curl commands to load the data necessary for the context
|
|
|
|
examples into an Elasticsearch cluster:
|
|
|
|
|
|
|
|
. Create {ref}/mapping.html[mappings] for the sample data.
|
|
|
|
+
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2019-07-31 11:21:46 -04:00
|
|
|
----
|
|
|
|
PUT /messages
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"mappings": {
|
|
|
|
"properties": {
|
|
|
|
"priority": {
|
|
|
|
"type": "integer"
|
|
|
|
},
|
|
|
|
"datetime": {
|
|
|
|
"type": "date"
|
|
|
|
},
|
|
|
|
"message": {
|
|
|
|
"type": "text"
|
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
+
|
|
|
|
. Load the sample data.
|
|
|
|
+
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2019-07-31 11:21:46 -04:00
|
|
|
----
|
|
|
|
POST /_bulk
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "1" } }
|
|
|
|
{ "priority": 1, "datetime": "2019-07-17T12:13:14Z", "message": "m1" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "2" } }
|
|
|
|
{ "priority": 1, "datetime": "2019-07-24T01:14:59Z", "message": "m2" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "3" } }
|
|
|
|
{ "priority": 2, "datetime": "1983-10-14T00:36:42Z", "message": "m3" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "4" } }
|
|
|
|
{ "priority": 3, "datetime": "1983-10-10T02:15:15Z", "message": "m4" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "5" } }
|
|
|
|
{ "priority": 3, "datetime": "1983-10-10T17:18:19Z", "message": "m5" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "6" } }
|
|
|
|
{ "priority": 1, "datetime": "2019-08-03T17:19:31Z", "message": "m6" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "7" } }
|
|
|
|
{ "priority": 3, "datetime": "2019-08-04T17:20:00Z", "message": "m7" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "8" } }
|
|
|
|
{ "priority": 2, "datetime": "2019-08-04T18:01:01Z", "message": "m8" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "9" } }
|
|
|
|
{ "priority": 3, "datetime": "1983-10-10T19:00:45Z", "message": "m9" }
|
|
|
|
{ "index" : { "_index" : "messages", "_id" : "10" } }
|
|
|
|
{ "priority": 2, "datetime": "2019-07-23T23:39:54Z", "message": "m10" }
|
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
===== Day-of-the-Week Bucket Aggregation Example
|
|
|
|
|
|
|
|
The following example uses a
|
|
|
|
{ref}/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script[terms aggregation]
|
|
|
|
as part of the
|
|
|
|
<<painless-bucket-script-agg-context, bucket script aggregation context>> to
|
|
|
|
display the number of messages from each day-of-the-week.
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2019-07-31 11:21:46 -04:00
|
|
|
----
|
|
|
|
GET /messages/_search?pretty=true
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"aggs": {
|
|
|
|
"day-of-week-count": {
|
|
|
|
"terms": {
|
|
|
|
"script": "return doc[\"datetime\"].value.getDayOfWeekEnum();"
|
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
===== Morning/Evening Bucket Aggregation Example
|
|
|
|
|
|
|
|
The following example uses a
|
|
|
|
{ref}/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script[terms aggregation]
|
|
|
|
as part of the
|
|
|
|
<<painless-bucket-script-agg-context, bucket script aggregation context>> to
|
|
|
|
display the number of messages received in the morning versus the evening.
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2019-07-31 11:21:46 -04:00
|
|
|
----
|
|
|
|
GET /messages/_search?pretty=true
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"aggs": {
|
|
|
|
"am-pm-count": {
|
|
|
|
"terms": {
|
|
|
|
"script": "return doc[\"datetime\"].value.getHour() < 12 ? \"AM\" : \"PM\";"
|
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
===== Age of a Message Script Field Example
|
|
|
|
|
|
|
|
The following example uses a
|
2020-08-06 13:06:06 -04:00
|
|
|
{ref}/search-fields.html#script-fields[script field] as part of the
|
2019-07-31 11:21:46 -04:00
|
|
|
<<painless-field-context, field context>> to display the elapsed time between
|
|
|
|
"now" and when a message was received.
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2019-07-31 11:21:46 -04:00
|
|
|
----
|
|
|
|
GET /_search?pretty=true
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"query": {
|
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"script_fields": {
|
|
|
|
"message_age": {
|
|
|
|
"script": {
|
|
|
|
"source": "ZonedDateTime now = ZonedDateTime.ofInstant(Instant.ofEpochMilli(params[\"now\"]), ZoneId.of(\"Z\")); ZonedDateTime mdt = doc[\"datetime\"].value; String age; long years = mdt.until(now, ChronoUnit.YEARS); age = years + \"Y \"; mdt = mdt.plusYears(years); long months = mdt.until(now, ChronoUnit.MONTHS); age += months + \"M \"; mdt = mdt.plusMonths(months); long days = mdt.until(now, ChronoUnit.DAYS); age += days + \"D \"; mdt = mdt.plusDays(days); long hours = mdt.until(now, ChronoUnit.HOURS); age += hours + \"h \"; mdt = mdt.plusHours(hours); long minutes = mdt.until(now, ChronoUnit.MINUTES); age += minutes + \"m \"; mdt = mdt.plusMinutes(minutes); long seconds = mdt.until(now, ChronoUnit.SECONDS); age += hours + \"s\"; return age;",
|
|
|
|
"params": {
|
|
|
|
"now": 1574005645830
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2019-07-31 11:21:46 -04:00
|
|
|
}
|
|
|
|
----
|
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
The following shows the script broken into multiple lines:
|
|
|
|
|
|
|
|
[source,Painless]
|
|
|
|
----
|
|
|
|
ZonedDateTime now = ZonedDateTime.ofInstant(
|
|
|
|
Instant.ofEpochMilli(params['now']), ZoneId.of('Z')); <1>
|
|
|
|
ZonedDateTime mdt = doc['datetime'].value; <2>
|
|
|
|
|
|
|
|
String age;
|
|
|
|
|
|
|
|
long years = mdt.until(now, ChronoUnit.YEARS); <3>
|
|
|
|
age = years + 'Y '; <4>
|
|
|
|
mdt = mdt.plusYears(years); <5>
|
|
|
|
|
|
|
|
long months = mdt.until(now, ChronoUnit.MONTHS);
|
|
|
|
age += months + 'M ';
|
|
|
|
mdt = mdt.plusMonths(months);
|
|
|
|
|
|
|
|
long days = mdt.until(now, ChronoUnit.DAYS);
|
|
|
|
age += days + 'D ';
|
|
|
|
mdt = mdt.plusDays(days);
|
|
|
|
|
|
|
|
long hours = mdt.until(now, ChronoUnit.HOURS);
|
|
|
|
age += hours + 'h ';
|
|
|
|
mdt = mdt.plusHours(hours);
|
|
|
|
|
|
|
|
long minutes = mdt.until(now, ChronoUnit.MINUTES);
|
|
|
|
age += minutes + 'm ';
|
|
|
|
mdt = mdt.plusMinutes(minutes);
|
|
|
|
|
|
|
|
long seconds = mdt.until(now, ChronoUnit.SECONDS);
|
|
|
|
age += hours + 's';
|
|
|
|
|
|
|
|
return age; <6>
|
|
|
|
----
|
|
|
|
<1> Parse the datetime "now" as input from the user-defined params.
|
|
|
|
<2> Store the datetime the message was received as a `ZonedDateTime`.
|
|
|
|
<3> Find the difference in years between "now" and the datetime the message was
|
|
|
|
received.
|
|
|
|
<4> Add the difference in years later returned in the format
|
|
|
|
`Y <years> ...` for the age of a message.
|
|
|
|
<5> Add the years so only the remainder of the months, days, etc. remain as the
|
|
|
|
difference between "now" and the datetime the message was received. Repeat this
|
|
|
|
pattern until the desired granularity is reached (seconds in this example).
|
|
|
|
<6> Return the age of the message in the format
|
|
|
|
`Y <years> M <months> D <days> h <hours> m <minutes> s <seconds>`.
|