From 34aca34e0084bcc13fb412b9857fb409ff650f5b Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Thu, 6 Sep 2018 15:26:12 +0200 Subject: [PATCH 1/5] Core: Add DateFormatter interface for java time parsing The existing approach used date formatters when a format based string like `date_time||epoch_millis` was used, instead of the custom code. In order to properly solve this, a new interface called `DateFormatter` has been added, which now can be implemented for custom formatters. Currently there are two implementations, one using java time and one doing the epoch_millis formatter, which simply parses a number and then converts it to a date in UTC timezone. The DateFormatter interface now also has a method to retrieve the name of the formatter, which is needed for mapping changes anyway. The existing `CompoundDateTimeFormatter` class has been removed, the name was not really nice anyway. One more minor change is the fact, that the new java time using FormatDateFormatter does not try to parse the date with its printer implementation first (which might be a strict one and fail), but a printer can now be specified in addition. This saves one potential failure/exception when parsing less strict dates. If only a printer is specified, the printer will also be used as a parser. --- .../ClusterAllocationExplanation.java | 2 +- .../cluster/metadata/IndexGraveyard.java | 7 +- .../cluster/routing/UnassignedInfo.java | 9 +- .../java/org/elasticsearch/common/Table.java | 6 +- .../common/time/DateFormatter.java | 93 +++++ .../common/time/DateFormatters.java | 341 +++++++++--------- .../common/time/DateMathParser.java | 8 +- .../common/time/EpochMillisDateFormatter.java | 60 +++ ...ormatter.java => FormatDateFormatter.java} | 77 ++-- .../XContentElasticsearchExtension.java | 20 +- .../elasticsearch/monitor/jvm/HotThreads.java | 6 +- .../rest/action/cat/RestIndicesAction.java | 2 +- .../rest/action/cat/RestShardsAction.java | 2 +- .../rest/action/cat/RestSnapshotAction.java | 8 +- .../rest/action/cat/RestTasksAction.java | 6 +- .../elasticsearch/snapshots/SnapshotInfo.java | 8 +- .../admin/indices/rollover/RolloverIT.java | 12 +- .../joda/JavaJodaTimeDuellingTests.java | 8 +- .../common/time/DateFormattersTests.java | 20 +- .../common/time/DateMathParserTests.java | 8 +- .../aggregations/bucket/MinDocCountIT.java | 2 +- .../bucket/histogram/DateHistogramTests.java | 4 +- .../search/fields/SearchFieldsIT.java | 10 +- 23 files changed, 437 insertions(+), 282 deletions(-) create mode 100644 server/src/main/java/org/elasticsearch/common/time/DateFormatter.java create mode 100644 server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java rename server/src/main/java/org/elasticsearch/common/time/{CompoundDateTimeFormatter.java => FormatDateFormatter.java} (58%) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java index cdf122545c4e2..d0386e0048710 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java @@ -191,7 +191,7 @@ private XContentBuilder unassignedInfoToXContent(UnassignedInfo unassignedInfo, builder.startObject("unassigned_info"); builder.field("reason", unassignedInfo.getReason()); builder.field("at", - UnassignedInfo.DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedInfo.getUnassignedTimeInMillis()))); + UnassignedInfo.DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(unassignedInfo.getUnassignedTimeInMillis()))); if (unassignedInfo.getNumFailedAllocations() > 0) { builder.field("failed_allocation_attempts", unassignedInfo.getNumFailedAllocations()); } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java index 8d0ad8efb7f5b..5814c89b2fb60 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java @@ -28,7 +28,7 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.xcontent.ContextParser; import org.elasticsearch.common.xcontent.ObjectParser; @@ -368,8 +368,7 @@ public static final class Tombstone implements ToXContentObject, Writeable { TOMBSTONE_PARSER.declareString((b, s) -> {}, new ParseField(DELETE_DATE_KEY)); } - static final CompoundDateTimeFormatter FORMATTER = - DateFormatters.forPattern("strict_date_optional_time").withZone(ZoneOffset.UTC); + static final DateFormatter FORMATTER = DateFormatters.forPattern("strict_date_optional_time").withZone(ZoneOffset.UTC); static ContextParser getParser() { return (parser, context) -> TOMBSTONE_PARSER.apply(parser, null).build(); @@ -434,7 +433,7 @@ public int hashCode() { @Override public String toString() { - String date = FORMATTER.format(Instant.ofEpochMilli(deleteDateInMillis)); + String date = FORMATTER.print(Instant.ofEpochMilli(deleteDateInMillis)); return "[index=" + index + ", deleteDate=" + date + "]"; } diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java b/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java index ad715500a9ecd..89a0ec31ed679 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java @@ -31,7 +31,7 @@ import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContentFragment; @@ -48,8 +48,7 @@ */ public final class UnassignedInfo implements ToXContentFragment, Writeable { - public static final CompoundDateTimeFormatter DATE_TIME_FORMATTER = - DateFormatters.forPattern("dateOptionalTime").withZone(ZoneOffset.UTC); + public static final DateFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("dateOptionalTime").withZone(ZoneOffset.UTC); public static final Setting INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING = Setting.positiveTimeSetting("index.unassigned.node_left.delayed_timeout", TimeValue.timeValueMinutes(1), Property.Dynamic, @@ -411,7 +410,7 @@ public static long findNextDelayedAllocation(long currentNanoTime, ClusterState public String shortSummary() { StringBuilder sb = new StringBuilder(); sb.append("[reason=").append(reason).append("]"); - sb.append(", at[").append(DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis))).append("]"); + sb.append(", at[").append(DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(unassignedTimeMillis))).append("]"); if (failedAllocations > 0) { sb.append(", failed_attempts[").append(failedAllocations).append("]"); } @@ -434,7 +433,7 @@ public String toString() { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("unassigned_info"); builder.field("reason", reason); - builder.field("at", DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis))); + builder.field("at", DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(unassignedTimeMillis))); if (failedAllocations > 0) { builder.field("failed_attempts", failedAllocations); } diff --git a/server/src/main/java/org/elasticsearch/common/Table.java b/server/src/main/java/org/elasticsearch/common/Table.java index 13d13066e16d6..9d0ec25d49e8d 100644 --- a/server/src/main/java/org/elasticsearch/common/Table.java +++ b/server/src/main/java/org/elasticsearch/common/Table.java @@ -19,7 +19,7 @@ package org.elasticsearch.common; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import java.time.Instant; @@ -85,7 +85,7 @@ public Table endHeaders() { return this; } - private static final CompoundDateTimeFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC); + private static final DateFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC); public Table startRow() { if (headers.isEmpty()) { @@ -95,7 +95,7 @@ public Table startRow() { if (withTime) { long time = System.currentTimeMillis(); addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)); - addCell(FORMATTER.format(Instant.ofEpochMilli(time))); + addCell(FORMATTER.print(Instant.ofEpochMilli(time))); } return this; } diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java new file mode 100644 index 0000000000000..e11da89edf912 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java @@ -0,0 +1,93 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.time; + +import java.time.ZoneId; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.TemporalField; +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +public interface DateFormatter { + + TemporalAccessor parse(String input); + + DateFormatter withZone(ZoneId zoneId); + + String print(TemporalAccessor accessor); + + String format(); + + DateFormatter parseDefaulting(Map fields); + + static DateFormatter merge(DateFormatter ... formatters) { + return new MergedDateFormatter(formatters); + } + + class MergedDateFormatter implements DateFormatter { + + private final String format; + private final DateFormatter[] formatters; + + MergedDateFormatter(DateFormatter ... formatters) { + this.formatters = formatters; + this.format = Arrays.stream(formatters).map(DateFormatter::format).collect(Collectors.joining("||")); + } + + @Override + public TemporalAccessor parse(String input) { + DateTimeParseException failure = null; + for (DateFormatter formatter : formatters) { + try { + return formatter.parse(input); + } catch (DateTimeParseException e) { + if (failure == null) { + failure = e; + } else { + failure.addSuppressed(e); + } + } + } + throw failure; + } + + @Override + public DateFormatter withZone(ZoneId zoneId) { + return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.withZone(zoneId)).toArray(DateFormatter[]::new)); + } + + @Override + public String print(TemporalAccessor accessor) { + return formatters[0].print(accessor); + } + + @Override + public String format() { + return format; + } + + @Override + public DateFormatter parseDefaulting(Map fields) { + return new MergedDateFormatter(Arrays.stream(formatters).map(f -> f.parseDefaulting(fields)).toArray(DateFormatter[]::new)); + } + } +} diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index 69b8cb0c85bfa..681abf238179d 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -25,12 +25,10 @@ import java.time.DayOfWeek; import java.time.Instant; import java.time.LocalDate; -import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; -import java.time.format.DateTimeParseException; import java.time.format.ResolverStyle; import java.time.format.SignStyle; import java.time.temporal.ChronoField; @@ -38,9 +36,6 @@ import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjusters; import java.time.temporal.WeekFields; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashSet; import java.util.Locale; import static java.time.temporal.ChronoField.DAY_OF_MONTH; @@ -106,8 +101,9 @@ public class DateFormatters { /** * Returns a generic ISO datetime parser where the date is mandatory and the time is optional. */ - private static final CompoundDateTimeFormatter STRICT_DATE_OPTIONAL_TIME = - new CompoundDateTimeFormatter(STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_2); + private static final DateFormatter STRICT_DATE_OPTIONAL_TIME = + new FormatDateFormatter("strict_date_optional_time", STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, + STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_2); private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1 = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -140,8 +136,9 @@ public class DateFormatters { /** * Returns a generic ISO datetime parser where the date is mandatory and the time is optional with nanosecond resolution. */ - private static final CompoundDateTimeFormatter STRICT_DATE_OPTIONAL_TIME_NANOS = - new CompoundDateTimeFormatter(STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_2); + private static final DateFormatter STRICT_DATE_OPTIONAL_TIME_NANOS = new FormatDateFormatter("strict_date_optional_time_nanos", + STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1, + STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_2); ///////////////////////////////////////// // @@ -162,7 +159,8 @@ public class DateFormatters { * Returns a basic formatter for a two digit hour of day, two digit minute * of hour, two digit second of minute, and time zone offset (HHmmssZ). */ - private static final CompoundDateTimeFormatter BASIC_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_TIME_NO_MILLIS = new FormatDateFormatter("basic_time_no_millis", + new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -186,7 +184,7 @@ public class DateFormatters { * of hour, two digit second of minute, three digit millis, and time zone * offset (HHmmss.SSSZ). */ - private static final CompoundDateTimeFormatter BASIC_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_TIME = new FormatDateFormatter("basic_time", new DateTimeFormatterBuilder().append(BASIC_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -203,7 +201,7 @@ public class DateFormatters { * of hour, two digit second of minute, three digit millis, and time zone * offset prefixed by 'T' ('T'HHmmss.SSSZ). */ - private static final CompoundDateTimeFormatter BASIC_T_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_T_TIME = new FormatDateFormatter("basic_t_time", new DateTimeFormatterBuilder().append(BASIC_T_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -214,11 +212,11 @@ public class DateFormatters { * of hour, two digit second of minute, and time zone offset prefixed by 'T' * ('T'HHmmssZ). */ - private static final CompoundDateTimeFormatter BASIC_T_TIME_NO_MILLIS = new CompoundDateTimeFormatter( - new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .appendZoneOrOffsetId().toFormatter(Locale.ROOT), - new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) - .append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) + private static final DateFormatter BASIC_T_TIME_NO_MILLIS = new FormatDateFormatter("basic_t_time_no_millis", + new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), + new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), + new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON) + .toFormatter(Locale.ROOT) ); private static final DateTimeFormatter BASIC_YEAR_MONTH_DAY_FORMATTER = new DateTimeFormatterBuilder() @@ -241,7 +239,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic date and time, separated * by a 'T' (yyyyMMdd'T'HHmmss.SSSZ). */ - private static final CompoundDateTimeFormatter BASIC_DATE_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_DATE_TIME = new FormatDateFormatter("basic_date_time", new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -254,7 +252,9 @@ public class DateFormatters { * Returns a basic formatter that combines a basic date and time without millis, * separated by a 'T' (yyyyMMdd'T'HHmmssZ). */ - private static final CompoundDateTimeFormatter BASIC_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_DATE_TIME_NO_MILLIS = new FormatDateFormatter("basic_t_time_no_millis", + new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) @@ -265,14 +265,14 @@ public class DateFormatters { * Returns a formatter for a full ordinal date, using a four * digit year and three digit dayOfYear (yyyyDDD). */ - private static final CompoundDateTimeFormatter BASIC_ORDINAL_DATE = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_ORDINAL_DATE = new FormatDateFormatter("basic_ordinal_date", DateTimeFormatter.ofPattern("yyyyDDD", Locale.ROOT)); /* * Returns a formatter for a full ordinal date and time, using a four * digit year and three digit dayOfYear (yyyyDDD'T'HHmmss.SSSZ). */ - private static final CompoundDateTimeFormatter BASIC_ORDINAL_DATE_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new FormatDateFormatter("basic_ordinal_date_time", new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) @@ -284,7 +284,9 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time without millis, * using a four digit year and three digit dayOfYear (yyyyDDD'T'HHmmssZ). */ - private static final CompoundDateTimeFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new FormatDateFormatter("basic_ordinal_date_time_no_millis", + new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) @@ -329,14 +331,14 @@ public class DateFormatters { * Returns a basic formatter for a full date as four digit weekyear, two * digit week of weekyear, and one digit day of week (xxxx'W'wwe). */ - private static final CompoundDateTimeFormatter STRICT_BASIC_WEEK_DATE = - new CompoundDateTimeFormatter(STRICT_BASIC_WEEK_DATE_PRINTER, STRICT_BASIC_WEEK_DATE_FORMATTER); + private static final DateFormatter STRICT_BASIC_WEEK_DATE = + new FormatDateFormatter("strict_basic_week_date", STRICT_BASIC_WEEK_DATE_PRINTER, STRICT_BASIC_WEEK_DATE_FORMATTER); /* * Returns a basic formatter that combines a basic weekyear date and time * without millis, separated by a 'T' (xxxx'W'wwe'T'HHmmssX). */ - private static final CompoundDateTimeFormatter STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_basic_week_date_no_millis", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER).append(DateTimeFormatter.ofPattern("'T'HHmmssX", Locale.ROOT)) .toFormatter(Locale.ROOT), @@ -349,7 +351,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic weekyear date and time, * separated by a 'T' (xxxx'W'wwe'T'HHmmss.SSSX). */ - private static final CompoundDateTimeFormatter STRICT_BASIC_WEEK_DATE_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME = new FormatDateFormatter("strict_basic_week_date_time", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER) .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", Locale.ROOT)) @@ -363,30 +365,32 @@ public class DateFormatters { /* * An ISO date formatter that formats or parses a date without an offset, such as '2011-12-03'. */ - private static final CompoundDateTimeFormatter STRICT_DATE = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_DATE = new FormatDateFormatter("strict_date", DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT)); /* * A date formatter that formats or parses a date plus an hour without an offset, such as '2011-12-03T01'. */ - private static final CompoundDateTimeFormatter STRICT_DATE_HOUR = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_DATE_HOUR = new FormatDateFormatter("strict_date_hour", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", Locale.ROOT)); /* * A date formatter that formats or parses a date plus an hour/minute without an offset, such as '2011-12-03T01:10'. */ - private static final CompoundDateTimeFormatter STRICT_DATE_HOUR_MINUTE = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_DATE_HOUR_MINUTE = new FormatDateFormatter("strict_date_hour_minute", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.ROOT)); /* * A strict date formatter that formats or parses a date without an offset, such as '2011-12-03'. */ - private static final CompoundDateTimeFormatter STRICT_YEAR_MONTH_DAY = new CompoundDateTimeFormatter(STRICT_YEAR_MONTH_DAY_FORMATTER); + private static final DateFormatter STRICT_YEAR_MONTH_DAY = + new FormatDateFormatter("strict_year_month_day", STRICT_YEAR_MONTH_DAY_FORMATTER); /* * A strict formatter that formats or parses a year and a month, such as '2011-12'. */ - private static final CompoundDateTimeFormatter STRICT_YEAR_MONTH = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder() + private static final DateFormatter STRICT_YEAR_MONTH = new FormatDateFormatter("strict_year_month", + new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral("-") .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) @@ -395,15 +399,15 @@ public class DateFormatters { /* * A strict formatter that formats or parses a year, such as '2011'. */ - private static final CompoundDateTimeFormatter STRICT_YEAR = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder() + private static final DateFormatter STRICT_YEAR = new FormatDateFormatter("strict_year", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .toFormatter(Locale.ROOT)); /* * A strict formatter that formats or parses a hour, minute and second, such as '09:43:25'. */ - private static final CompoundDateTimeFormatter STRICT_HOUR_MINUTE_SECOND = - new CompoundDateTimeFormatter(STRICT_HOUR_MINUTE_SECOND_FORMATTER); + private static final DateFormatter STRICT_HOUR_MINUTE_SECOND = + new FormatDateFormatter("strict_hour_minute_second", STRICT_HOUR_MINUTE_SECOND_FORMATTER); private static final DateTimeFormatter STRICT_DATE_FORMATTER = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -418,7 +422,8 @@ public class DateFormatters { * Returns a formatter that combines a full date and time, separated by a 'T' * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter STRICT_DATE_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_DATE_TIME = new FormatDateFormatter("strict_date_time", + new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -435,7 +440,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time without millis, * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -452,7 +457,9 @@ public class DateFormatters { * Returns a formatter that combines a full date and time without millis, * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter STRICT_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_date_time_no_millis", + new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) @@ -478,17 +485,19 @@ public class DateFormatters { * NOTE: this is not a strict formatter to retain the joda time based behaviour, * even though it's named like this */ - private static final CompoundDateTimeFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS = - new CompoundDateTimeFormatter(STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, STRICT_HOUR_MINUTE_SECOND_MILLIS_FORMATTER); + private static final DateFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS = + new FormatDateFormatter("strict_hour_minute_second_millis", + STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, STRICT_HOUR_MINUTE_SECOND_MILLIS_FORMATTER); - private static final CompoundDateTimeFormatter STRICT_HOUR_MINUTE_SECOND_FRACTION = STRICT_HOUR_MINUTE_SECOND_MILLIS; + private static final DateFormatter STRICT_HOUR_MINUTE_SECOND_FRACTION = STRICT_HOUR_MINUTE_SECOND_MILLIS; /* * Returns a formatter that combines a full date, two digit hour of day, * two digit minute of hour, two digit second of minute, and three digit * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). */ - private static final CompoundDateTimeFormatter STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION = new FormatDateFormatter( + "strict_date_hour_minute_second_fraction", new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") @@ -503,20 +512,20 @@ public class DateFormatters { .toFormatter(Locale.ROOT) ); - private static final CompoundDateTimeFormatter STRICT_DATE_HOUR_MINUTE_SECOND_MILLIS = STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION; + private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_MILLIS = STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION; /* * Returns a formatter for a two digit hour of day. (HH) */ - private static final CompoundDateTimeFormatter STRICT_HOUR = - new CompoundDateTimeFormatter(DateTimeFormatter.ofPattern("HH", Locale.ROOT)); + private static final DateFormatter STRICT_HOUR = + new FormatDateFormatter("strict_hour", DateTimeFormatter.ofPattern("HH", Locale.ROOT)); /* * Returns a formatter for a two digit hour of day and two digit minute of * hour. (HH:mm) */ - private static final CompoundDateTimeFormatter STRICT_HOUR_MINUTE = - new CompoundDateTimeFormatter(DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT)); + private static final DateFormatter STRICT_HOUR_MINUTE = + new FormatDateFormatter("strict_hour_minute", DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT)); private static final DateTimeFormatter STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) @@ -535,7 +544,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time, using a four * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter STRICT_ORDINAL_DATE_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_ORDINAL_DATE_TIME = new FormatDateFormatter("strict_ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) @@ -566,7 +575,7 @@ public class DateFormatters { * hour, two digit second of minute, three digit fraction of second, and * time zone offset (HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter STRICT_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_TIME = new FormatDateFormatter("strict_time", new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -577,7 +586,7 @@ public class DateFormatters { * hour, two digit second of minute, three digit fraction of second, and * time zone offset prefixed by 'T' ('T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter STRICT_T_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_T_TIME = new FormatDateFormatter("strict_t_time", new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -597,7 +606,8 @@ public class DateFormatters { * Returns a formatter for a two digit hour of day, two digit minute of * hour, two digit second of minute, and time zone offset (HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter STRICT_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_TIME_NO_MILLIS = new FormatDateFormatter("strict_time_no_millis", + new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -607,7 +617,9 @@ public class DateFormatters { * hour, two digit second of minute, and time zone offset prefixed * by 'T' ('T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter STRICT_T_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_T_TIME_NO_MILLIS = new FormatDateFormatter("strict_t_time_no_millis", + new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) @@ -632,13 +644,15 @@ public class DateFormatters { * Returns a formatter for a full date as four digit weekyear, two digit * week of weekyear, and one digit day of week (xxxx-'W'ww-e). */ - private static final CompoundDateTimeFormatter STRICT_WEEK_DATE = new CompoundDateTimeFormatter(ISO_WEEK_DATE); + private static final DateFormatter STRICT_WEEK_DATE = new FormatDateFormatter("strict_week_date", ISO_WEEK_DATE); /* * Returns a formatter that combines a full weekyear date and time without millis, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter STRICT_WEEK_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_week_date_time_no_millis", + new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) + .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) @@ -649,7 +663,7 @@ public class DateFormatters { * Returns a formatter that combines a full weekyear date and time, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter STRICT_WEEK_DATE_TIME = new CompoundDateTimeFormatter( + private static final DateFormatter STRICT_WEEK_DATE_TIME = new FormatDateFormatter("strict_week_date_time", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -660,7 +674,7 @@ public class DateFormatters { /* * Returns a formatter for a four digit weekyear */ - private static final CompoundDateTimeFormatter STRICT_WEEKYEAR = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder() + private static final DateFormatter STRICT_WEEKYEAR = new FormatDateFormatter("strict_weekyear", new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear(), 4, 10, SignStyle.EXCEEDS_PAD) .toFormatter(Locale.ROOT)); @@ -674,13 +688,15 @@ public class DateFormatters { * Returns a formatter for a four digit weekyear and two digit week of * weekyear. (xxxx-'W'ww) */ - private static final CompoundDateTimeFormatter STRICT_WEEKYEAR_WEEK = new CompoundDateTimeFormatter(STRICT_WEEKYEAR_WEEK_FORMATTER); + private static final DateFormatter STRICT_WEEKYEAR_WEEK = + new FormatDateFormatter("strict_weekyear_week", STRICT_WEEKYEAR_WEEK_FORMATTER); /* * Returns a formatter for a four digit weekyear, two digit week of * weekyear, and one digit day of week. (xxxx-'W'ww-e) */ - private static final CompoundDateTimeFormatter STRICT_WEEKYEAR_WEEK_DAY = new CompoundDateTimeFormatter(new DateTimeFormatterBuilder() + private static final DateFormatter STRICT_WEEKYEAR_WEEK_DAY = new FormatDateFormatter("strict_weekyear_week_day", + new DateTimeFormatterBuilder() .append(STRICT_WEEKYEAR_WEEK_FORMATTER) .appendLiteral("-") .appendValue(WeekFields.ISO.dayOfWeek()) @@ -691,14 +707,14 @@ public class DateFormatters { * two digit minute of hour, and two digit second of * minute. (yyyy-MM-dd'T'HH:mm:ss) */ - private static final CompoundDateTimeFormatter STRICT_DATE_HOUR_MINUTE_SECOND = - new CompoundDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT)); + private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND = new FormatDateFormatter("strict_date_hour_minute_second", + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT)); /* * A basic formatter for a full date as four digit year, two digit * month of year, and two digit day of month (yyyyMMdd). */ - private static final CompoundDateTimeFormatter BASIC_DATE = new CompoundDateTimeFormatter( + private static final DateFormatter BASIC_DATE = new FormatDateFormatter("basic_date", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NORMAL) .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) @@ -723,7 +739,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date, using a four * digit year and three digit dayOfYear (yyyy-DDD). */ - private static final CompoundDateTimeFormatter STRICT_ORDINAL_DATE = new CompoundDateTimeFormatter(STRICT_ORDINAL_DATE_FORMATTER); + private static final DateFormatter STRICT_ORDINAL_DATE = new FormatDateFormatter("strict_ordinal_date", STRICT_ORDINAL_DATE_FORMATTER); ///////////////////////////////////////// // @@ -759,7 +775,8 @@ public class DateFormatters { * a date formatter with optional time, being very lenient, format is * yyyy-MM-dd'T'HH:mm:ss.SSSZ */ - private static final CompoundDateTimeFormatter DATE_OPTIONAL_TIME = new CompoundDateTimeFormatter(STRICT_DATE_OPTIONAL_TIME.printer, + private static final DateFormatter DATE_OPTIONAL_TIME = new FormatDateFormatter("date_optional_time", + STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .optionalStart() @@ -834,8 +851,8 @@ public class DateFormatters { * Returns a formatter for a full ordinal date, using a four * digit year and three digit dayOfYear (yyyy-DDD). */ - private static final CompoundDateTimeFormatter ORDINAL_DATE = - new CompoundDateTimeFormatter(ORDINAL_DATE_PRINTER, ORDINAL_DATE_FORMATTER); + private static final DateFormatter ORDINAL_DATE = + new FormatDateFormatter("ordinal_date", ORDINAL_DATE_PRINTER, ORDINAL_DATE_FORMATTER); private static final DateTimeFormatter TIME_NO_MILLIS_FORMATTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE) @@ -864,70 +881,32 @@ public class DateFormatters { /* * Returns a formatter for a four digit weekyear. (YYYY) */ - private static final CompoundDateTimeFormatter WEEK_YEAR = new CompoundDateTimeFormatter( + private static final DateFormatter WEEK_YEAR = new FormatDateFormatter("week_year", new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(Locale.ROOT)); /* * Returns a formatter for a four digit weekyear. (uuuu) */ - private static final CompoundDateTimeFormatter YEAR = new CompoundDateTimeFormatter( + private static final DateFormatter YEAR = new FormatDateFormatter("year", new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(Locale.ROOT)); /* * Returns a formatter for parsing the seconds since the epoch */ - private static final CompoundDateTimeFormatter EPOCH_SECOND = new CompoundDateTimeFormatter( + private static final DateFormatter EPOCH_SECOND = new FormatDateFormatter("epoch_second", new DateTimeFormatterBuilder().appendValue(ChronoField.INSTANT_SECONDS).toFormatter(Locale.ROOT)); /* - * Returns a formatter for parsing the milliseconds since the epoch - * This one needs a custom implementation, because the standard date formatter can not parse negative values - * or anything +- 999 milliseconds around the epoch - * - * This implementation just resorts to parsing the input directly to an Instant by trying to parse a number. + * Parses the milliseconds since/before the epoch */ - private static final DateTimeFormatter EPOCH_MILLIS_FORMATTER = new DateTimeFormatterBuilder() - .appendValue(ChronoField.INSTANT_SECONDS, 1, 19, SignStyle.NEVER) - .appendValue(ChronoField.MILLI_OF_SECOND, 3) - .toFormatter(Locale.ROOT); - - private static final class EpochDateTimeFormatter extends CompoundDateTimeFormatter { - - private EpochDateTimeFormatter() { - super(EPOCH_MILLIS_FORMATTER); - } - - private EpochDateTimeFormatter(ZoneId zoneId) { - super(EPOCH_MILLIS_FORMATTER.withZone(zoneId)); - } - - @Override - public TemporalAccessor parse(String input) { - try { - return Instant.ofEpochMilli(Long.valueOf(input)).atZone(ZoneOffset.UTC); - } catch (NumberFormatException e) { - throw new DateTimeParseException("invalid number", input, 0, e); - } - } - - @Override - public CompoundDateTimeFormatter withZone(ZoneId zoneId) { - return new EpochDateTimeFormatter(zoneId); - } - - @Override - public String format(TemporalAccessor accessor) { - return String.valueOf(Instant.from(accessor).toEpochMilli()); - } - } - - private static final CompoundDateTimeFormatter EPOCH_MILLIS = new EpochDateTimeFormatter(); + private static final DateFormatter EPOCH_MILLIS = new EpochMillisDateFormatter(); /* * Returns a formatter that combines a full date and two digit hour of * day. (yyyy-MM-dd'T'HH) */ - private static final CompoundDateTimeFormatter DATE_HOUR = new CompoundDateTimeFormatter(STRICT_DATE_HOUR.printer, + private static final DateFormatter DATE_HOUR = new FormatDateFormatter("date_hour", + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", Locale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -940,8 +919,8 @@ public String format(TemporalAccessor accessor) { * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). Parsing will parse up * to 3 fractional second digits. */ - private static final CompoundDateTimeFormatter DATE_HOUR_MINUTE_SECOND_MILLIS = - new CompoundDateTimeFormatter( + private static final DateFormatter DATE_HOUR_MINUTE_SECOND_MILLIS = + new FormatDateFormatter("date_hour_minute_second_millis", new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") @@ -953,13 +932,14 @@ public String format(TemporalAccessor accessor) { .append(HOUR_MINUTE_SECOND_MILLIS_FORMATTER) .toFormatter(Locale.ROOT)); - private static final CompoundDateTimeFormatter DATE_HOUR_MINUTE_SECOND_FRACTION = DATE_HOUR_MINUTE_SECOND_MILLIS; + private static final DateFormatter DATE_HOUR_MINUTE_SECOND_FRACTION = DATE_HOUR_MINUTE_SECOND_MILLIS; /* * Returns a formatter that combines a full date, two digit hour of day, * and two digit minute of hour. (yyyy-MM-dd'T'HH:mm) */ - private static final CompoundDateTimeFormatter DATE_HOUR_MINUTE = new CompoundDateTimeFormatter(STRICT_DATE_HOUR_MINUTE.printer, + private static final DateFormatter DATE_HOUR_MINUTE = new FormatDateFormatter("date_hour_minute", + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -971,8 +951,8 @@ public String format(TemporalAccessor accessor) { * two digit minute of hour, and two digit second of * minute. (yyyy-MM-dd'T'HH:mm:ss) */ - private static final CompoundDateTimeFormatter DATE_HOUR_MINUTE_SECOND = new CompoundDateTimeFormatter( - STRICT_DATE_HOUR_MINUTE_SECOND.printer, + private static final DateFormatter DATE_HOUR_MINUTE_SECOND = new FormatDateFormatter("date_hour_minute_second", + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) .appendLiteral("T") @@ -994,8 +974,8 @@ public String format(TemporalAccessor accessor) { * Returns a formatter that combines a full date and time, separated by a 'T' * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter DATE_TIME = new CompoundDateTimeFormatter( - STRICT_DATE_TIME.printer, + private static final DateFormatter DATE_TIME = new FormatDateFormatter("date_time", + STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, new DateTimeFormatterBuilder().append(DATE_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(DATE_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -1004,20 +984,22 @@ public String format(TemporalAccessor accessor) { * Returns a basic formatter for a full date as four digit weekyear, two * digit week of weekyear, and one digit day of week (YYYY'W'wwe). */ - private static final CompoundDateTimeFormatter BASIC_WEEK_DATE = - new CompoundDateTimeFormatter(STRICT_BASIC_WEEK_DATE.printer, BASIC_WEEK_DATE_FORMATTER); + private static final DateFormatter BASIC_WEEK_DATE = + new FormatDateFormatter("basic_week_date", STRICT_BASIC_WEEK_DATE_PRINTER, BASIC_WEEK_DATE_FORMATTER); /* * Returns a formatter for a full date as four digit year, two digit month * of year, and two digit day of month (yyyy-MM-dd). */ - private static final CompoundDateTimeFormatter DATE = new CompoundDateTimeFormatter(STRICT_DATE.printer, DATE_FORMATTER); + private static final DateFormatter DATE = new FormatDateFormatter("date", + DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT), + DATE_FORMATTER); // only the formatter, nothing optional here private static final DateTimeFormatter DATE_TIME_NO_MILLIS_PRINTER = new DateTimeFormatterBuilder() - .append(STRICT_DATE.printer) + .append(DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT)) .appendLiteral('T') - .append(STRICT_HOUR_MINUTE.printer) + .appendPattern("HH:mm") .appendLiteral(':') .appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE) .appendZoneId() @@ -1037,7 +1019,8 @@ public String format(TemporalAccessor accessor) { * Returns a formatter that combines a full date and time without millis, but with a timezone that can be optional * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZ). */ - private static final CompoundDateTimeFormatter DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter(DATE_TIME_NO_MILLIS_PRINTER, + private static final DateFormatter DATE_TIME_NO_MILLIS = new FormatDateFormatter("date_time_no_millis", + DATE_TIME_NO_MILLIS_PRINTER, new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX) @@ -1051,21 +1034,21 @@ public String format(TemporalAccessor accessor) { * hour, two digit second of minute, and three digit fraction of * second (HH:mm:ss.SSS). */ - private static final CompoundDateTimeFormatter HOUR_MINUTE_SECOND_MILLIS = - new CompoundDateTimeFormatter(STRICT_HOUR_MINUTE_SECOND_FRACTION.printer, HOUR_MINUTE_SECOND_MILLIS_FORMATTER); + private static final DateFormatter HOUR_MINUTE_SECOND_MILLIS = new FormatDateFormatter("hour_minute_second_millis", + STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, HOUR_MINUTE_SECOND_MILLIS_FORMATTER); /* * Returns a formatter for a two digit hour of day and two digit minute of * hour. (HH:mm) */ - private static final CompoundDateTimeFormatter HOUR_MINUTE = - new CompoundDateTimeFormatter(STRICT_HOUR_MINUTE.printer, HOUR_MINUTE_FORMATTER); + private static final DateFormatter HOUR_MINUTE = + new FormatDateFormatter("hour_minute", DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT), HOUR_MINUTE_FORMATTER); /* * A strict formatter that formats or parses a hour, minute and second, such as '09:43:25'. */ - private static final CompoundDateTimeFormatter HOUR_MINUTE_SECOND = new CompoundDateTimeFormatter( - STRICT_HOUR_MINUTE_SECOND.printer, + private static final DateFormatter HOUR_MINUTE_SECOND = new FormatDateFormatter("hour_minute_second", + STRICT_HOUR_MINUTE_SECOND_FORMATTER, new DateTimeFormatterBuilder() .append(HOUR_MINUTE_FORMATTER) .appendLiteral(":") @@ -1076,8 +1059,8 @@ public String format(TemporalAccessor accessor) { /* * Returns a formatter for a two digit hour of day. (HH) */ - private static final CompoundDateTimeFormatter HOUR = new CompoundDateTimeFormatter( - STRICT_HOUR.printer, + private static final DateFormatter HOUR = new FormatDateFormatter("hour", + DateTimeFormatter.ofPattern("HH", Locale.ROOT), new DateTimeFormatterBuilder().appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE).toFormatter(Locale.ROOT) ); @@ -1096,8 +1079,9 @@ public String format(TemporalAccessor accessor) { * Returns a formatter for a full ordinal date and time, using a four * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter ORDINAL_DATE_TIME = new CompoundDateTimeFormatter( - STRICT_ORDINAL_DATE_TIME.printer, + private static final DateFormatter ORDINAL_DATE_TIME = new FormatDateFormatter("ordinal_date_time", + new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_FORMATTER_BASE) @@ -1114,8 +1098,9 @@ public String format(TemporalAccessor accessor) { * Returns a formatter for a full ordinal date and time without millis, * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter ORDINAL_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( - STRICT_ORDINAL_DATE_TIME_NO_MILLIS.printer, + private static final DateFormatter ORDINAL_DATE_TIME_NO_MILLIS = new FormatDateFormatter("ordinal_date_time_no_millis", + new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -1126,8 +1111,8 @@ public String format(TemporalAccessor accessor) { * Returns a formatter that combines a full weekyear date and time, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter WEEK_DATE_TIME = new CompoundDateTimeFormatter( - STRICT_WEEK_DATE_TIME.printer, + private static final DateFormatter WEEK_DATE_TIME = new FormatDateFormatter("week_date_time", + new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).appendLiteral("T").append(TIME_PREFIX) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).appendLiteral("T").append(TIME_PREFIX) @@ -1138,8 +1123,9 @@ public String format(TemporalAccessor accessor) { * Returns a formatter that combines a full weekyear date and time, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter WEEK_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( - STRICT_WEEK_DATE_TIME_NO_MILLIS.printer, + private static final DateFormatter WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("week_date_time_no_millis", + new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) + .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).append(T_TIME_NO_MILLIS_FORMATTER) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).append(T_TIME_NO_MILLIS_FORMATTER) @@ -1150,8 +1136,11 @@ public String format(TemporalAccessor accessor) { * Returns a basic formatter that combines a basic weekyear date and time, * separated by a 'T' (xxxx'W'wwe'T'HHmmss.SSSX). */ - private static final CompoundDateTimeFormatter BASIC_WEEK_DATE_TIME = new CompoundDateTimeFormatter( - STRICT_BASIC_WEEK_DATE_TIME.printer, + private static final DateFormatter BASIC_WEEK_DATE_TIME = new FormatDateFormatter("basic_week_date_time", + new DateTimeFormatterBuilder() + .append(STRICT_BASIC_WEEK_DATE_PRINTER) + .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", Locale.ROOT)) + .toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).append(BASIC_T_TIME_FORMATTER) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).append(BASIC_T_TIME_FORMATTER) @@ -1162,8 +1151,10 @@ public String format(TemporalAccessor accessor) { * Returns a basic formatter that combines a basic weekyear date and time, * separated by a 'T' (xxxx'W'wwe'T'HHmmssX). */ - private static final CompoundDateTimeFormatter BASIC_WEEK_DATE_TIME_NO_MILLIS = new CompoundDateTimeFormatter( - STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS.printer, + private static final DateFormatter BASIC_WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("basic_week_date_time_no_millis", + new DateTimeFormatterBuilder() + .append(STRICT_BASIC_WEEK_DATE_PRINTER).append(DateTimeFormatter.ofPattern("'T'HHmmssX", Locale.ROOT)) + .toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_WEEK_DATE_FORMATTER).appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) @@ -1175,8 +1166,8 @@ public String format(TemporalAccessor accessor) { * hour, two digit second of minute, three digit fraction of second, and * time zone offset (HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter TIME = new CompoundDateTimeFormatter( - STRICT_TIME.printer, + private static final DateFormatter TIME = new FormatDateFormatter("time", + new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_PREFIX).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_PREFIX).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -1185,8 +1176,8 @@ public String format(TemporalAccessor accessor) { * Returns a formatter for a two digit hour of day, two digit minute of * hour, two digit second of minute, andtime zone offset (HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter TIME_NO_MILLIS = new CompoundDateTimeFormatter( - STRICT_TIME_NO_MILLIS.printer, + private static final DateFormatter TIME_NO_MILLIS = new FormatDateFormatter("time_no_millis", + new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -1196,8 +1187,8 @@ public String format(TemporalAccessor accessor) { * hour, two digit second of minute, three digit fraction of second, and * time zone offset prefixed by 'T' ('T'HH:mm:ss.SSSZZ). */ - private static final CompoundDateTimeFormatter T_TIME = new CompoundDateTimeFormatter( - STRICT_T_TIME.printer, + private static final DateFormatter T_TIME = new FormatDateFormatter("t_time", + new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_PREFIX) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_PREFIX) @@ -1209,8 +1200,9 @@ public String format(TemporalAccessor accessor) { * hour, two digit second of minute, and time zone offset prefixed * by 'T' ('T'HH:mm:ssZZ). */ - private static final CompoundDateTimeFormatter T_TIME_NO_MILLIS = new CompoundDateTimeFormatter( - STRICT_T_TIME_NO_MILLIS.printer, + private static final DateFormatter T_TIME_NO_MILLIS = new FormatDateFormatter("t_time_no_millis", + new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) + .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(T_TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(T_TIME_NO_MILLIS_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) ); @@ -1218,16 +1210,20 @@ public String format(TemporalAccessor accessor) { /* * A strict formatter that formats or parses a year and a month, such as '2011-12'. */ - private static final CompoundDateTimeFormatter YEAR_MONTH = new CompoundDateTimeFormatter( - STRICT_YEAR_MONTH.printer, + private static final DateFormatter YEAR_MONTH = new FormatDateFormatter("year_month", + new DateTimeFormatterBuilder() + .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) + .appendLiteral("-") + .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) + .toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).appendLiteral("-").appendValue(MONTH_OF_YEAR).toFormatter(Locale.ROOT) ); /* * A strict date formatter that formats or parses a date without an offset, such as '2011-12-03'. */ - private static final CompoundDateTimeFormatter YEAR_MONTH_DAY = new CompoundDateTimeFormatter( - STRICT_YEAR_MONTH_DAY.printer, + private static final DateFormatter YEAR_MONTH_DAY = new FormatDateFormatter("year_month_day", + STRICT_YEAR_MONTH_DAY_FORMATTER, new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) .appendLiteral("-") @@ -1241,13 +1237,13 @@ public String format(TemporalAccessor accessor) { * Returns a formatter for a full date as four digit weekyear, two digit * week of weekyear, and one digit day of week (xxxx-'W'ww-e). */ - private static final CompoundDateTimeFormatter WEEK_DATE = new CompoundDateTimeFormatter(STRICT_WEEK_DATE.printer, WEEK_DATE_FORMATTER); + private static final DateFormatter WEEK_DATE = new FormatDateFormatter("week_date", ISO_WEEK_DATE, WEEK_DATE_FORMATTER); /* * Returns a formatter for a four digit weekyear and two digit week of * weekyear. (xxxx-'W'ww) */ - private static final CompoundDateTimeFormatter WEEKYEAR_WEEK = new CompoundDateTimeFormatter(STRICT_WEEKYEAR_WEEK.printer, + private static final DateFormatter WEEKYEAR_WEEK = new FormatDateFormatter("weekyear_week", STRICT_WEEKYEAR_WEEK_FORMATTER, new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear()) .appendLiteral("-W") @@ -1259,8 +1255,12 @@ public String format(TemporalAccessor accessor) { * Returns a formatter for a four digit weekyear, two digit week of * weekyear, and one digit day of week. (xxxx-'W'ww-e) */ - private static final CompoundDateTimeFormatter WEEKYEAR_WEEK_DAY = new CompoundDateTimeFormatter( - STRICT_WEEKYEAR_WEEK_DAY.printer, + private static final DateFormatter WEEKYEAR_WEEK_DAY = new FormatDateFormatter("weekyear_week_day", + new DateTimeFormatterBuilder() + .append(STRICT_WEEKYEAR_WEEK_FORMATTER) + .appendLiteral("-") + .appendValue(WeekFields.ISO.dayOfWeek()) + .toFormatter(Locale.ROOT), new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear()) .appendLiteral("-W") @@ -1276,11 +1276,11 @@ public String format(TemporalAccessor accessor) { // ///////////////////////////////////////// - public static CompoundDateTimeFormatter forPattern(String input) { + public static DateFormatter forPattern(String input) { return forPattern(input, Locale.ROOT); } - public static CompoundDateTimeFormatter forPattern(String input, Locale locale) { + public static DateFormatter forPattern(String input, Locale locale) { if (Strings.hasLength(input)) { input = input.trim(); } @@ -1452,21 +1452,20 @@ public static CompoundDateTimeFormatter forPattern(String input, Locale locale) if (formats.length == 1) { return forPattern(formats[0], locale); } else { - Collection parsers = new LinkedHashSet<>(formats.length); - for (String format : formats) { - CompoundDateTimeFormatter dateTimeFormatter = forPattern(format, locale); - try { - parsers.addAll(Arrays.asList(dateTimeFormatter.parsers)); - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e); + try { + DateFormatter[] formatters = new DateFormatter[formats.length]; + for (int i = 0; i < formats.length; i++) { + formatters[i] = forPattern(formats[i], locale); } - } - return new CompoundDateTimeFormatter(parsers.toArray(new DateTimeFormatter[0])); + return DateFormatter.merge(formatters); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e); + } } } else { try { - return new CompoundDateTimeFormatter(new DateTimeFormatterBuilder().appendPattern(input).toFormatter(locale)); + return new FormatDateFormatter(input, new DateTimeFormatterBuilder().appendPattern(input).toFormatter(locale)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e); } diff --git a/server/src/main/java/org/elasticsearch/common/time/DateMathParser.java b/server/src/main/java/org/elasticsearch/common/time/DateMathParser.java index 39f6dabbdb2e6..5e5ecc5bafd9a 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateMathParser.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateMathParser.java @@ -58,10 +58,10 @@ public class DateMathParser { ROUND_UP_BASE_FIELDS.put(ChronoField.MILLI_OF_SECOND, 999L); } - private final CompoundDateTimeFormatter formatter; - private final CompoundDateTimeFormatter roundUpFormatter; + private final DateFormatter formatter; + private final DateFormatter roundUpFormatter; - public DateMathParser(CompoundDateTimeFormatter formatter) { + public DateMathParser(DateFormatter formatter) { Objects.requireNonNull(formatter); this.formatter = formatter; this.roundUpFormatter = formatter.parseDefaulting(ROUND_UP_BASE_FIELDS); @@ -247,7 +247,7 @@ private long parseMath(final String mathString, final long time, final boolean r } private long parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNoTime) { - CompoundDateTimeFormatter formatter = roundUpIfNoTime ? this.roundUpFormatter : this.formatter; + DateFormatter formatter = roundUpIfNoTime ? this.roundUpFormatter : this.formatter; try { if (timeZone == null) { return DateFormatters.toZonedDateTime(formatter.parse(value)).toInstant().toEpochMilli(); diff --git a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java new file mode 100644 index 0000000000000..f6a6bd986d43d --- /dev/null +++ b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java @@ -0,0 +1,60 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.time; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; +import java.time.temporal.TemporalField; +import java.util.Map; + +public class EpochMillisDateFormatter implements DateFormatter { + + @Override + public TemporalAccessor parse(String input) { + try { + return Instant.ofEpochMilli(Long.valueOf(input)).atZone(ZoneOffset.UTC); + } catch (NumberFormatException e) { + throw new DateTimeParseException("invalid number", input, 0, e); + } + } + + @Override + public DateFormatter withZone(ZoneId zoneId) { + return this; + } + + @Override + public String print(TemporalAccessor accessor) { + return String.valueOf(Instant.from(accessor).toEpochMilli()); + } + + @Override + public String format() { + return "epoch_millis"; + } + + @Override + public DateFormatter parseDefaulting(Map fields) { + return this; + } +} diff --git a/server/src/main/java/org/elasticsearch/common/time/CompoundDateTimeFormatter.java b/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java similarity index 58% rename from server/src/main/java/org/elasticsearch/common/time/CompoundDateTimeFormatter.java rename to server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java index 0332c03814de7..0809a04c193b4 100644 --- a/server/src/main/java/org/elasticsearch/common/time/CompoundDateTimeFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.elasticsearch.common.time; import java.time.ZoneId; @@ -27,33 +28,28 @@ import java.util.Arrays; import java.util.Locale; import java.util.Map; -import java.util.function.Consumer; -/** - * wrapper class around java.time.DateTimeFormatter that supports multiple formats for easier parsing, - * and one specific format for printing - */ -public class CompoundDateTimeFormatter { +public class FormatDateFormatter implements DateFormatter { - private static final Consumer SAME_TIME_ZONE_VALIDATOR = (parsers) -> { + private final String format; + private final DateTimeFormatter printer; + private final DateTimeFormatter[] parsers; + + public FormatDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { long distinctZones = Arrays.stream(parsers).map(DateTimeFormatter::getZone).distinct().count(); if (distinctZones > 1) { throw new IllegalArgumentException("formatters must have the same time zone"); } - }; - - final DateTimeFormatter printer; - final DateTimeFormatter[] parsers; - - CompoundDateTimeFormatter(DateTimeFormatter ... parsers) { if (parsers.length == 0) { - throw new IllegalArgumentException("at least one date time formatter is required"); + this.parsers = new DateTimeFormatter[]{printer}; + } else { + this.parsers = parsers; } - SAME_TIME_ZONE_VALIDATOR.accept(parsers); - this.printer = parsers[0]; - this.parsers = parsers; + this.format = format; + this.printer = printer; } + @Override public TemporalAccessor parse(String input) { DateTimeParseException failure = null; for (int i = 0; i < parsers.length; i++) { @@ -72,13 +68,8 @@ public TemporalAccessor parse(String input) { throw failure; } - /** - * Configure a specific time zone for a date formatter - * - * @param zoneId The zoneId this formatter shoulduse - * @return The new formatter with all parsers switched to the specified timezone - */ - public CompoundDateTimeFormatter withZone(ZoneId zoneId) { + @Override + public DateFormatter withZone(ZoneId zoneId) { // shortcurt to not create new objects unnecessarily if (zoneId.equals(parsers[0].getZone())) { return this; @@ -89,25 +80,33 @@ public CompoundDateTimeFormatter withZone(ZoneId zoneId) { parsersWithZone[i] = parsers[i].withZone(zoneId); } - return new CompoundDateTimeFormatter(parsersWithZone); + return new FormatDateFormatter(format, printer.withZone(zoneId), parsersWithZone); } - /** - * Configure defaults for missing values in a parser, then return a new compound date formatter - */ - CompoundDateTimeFormatter parseDefaulting(Map fields) { - final DateTimeFormatter[] parsersWithDefaulting = new DateTimeFormatter[parsers.length]; - for (int i = 0; i < parsers.length; i++) { - DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(parsers[i]); - fields.forEach(builder::parseDefaulting); - parsersWithDefaulting[i] = builder.toFormatter(Locale.ROOT); - } - - return new CompoundDateTimeFormatter(parsersWithDefaulting); + @Override + public String print(TemporalAccessor accessor) { + return printer.format(accessor); } - public String format(TemporalAccessor accessor) { - return printer.format(accessor); + @Override + public String format() { + return format; } + @Override + public DateFormatter parseDefaulting(Map fields) { + final DateTimeFormatterBuilder parseDefaultingBuilder = new DateTimeFormatterBuilder().append(printer); + fields.forEach(parseDefaultingBuilder::parseDefaulting); + if (parsers.length == 1 && parsers[0].equals(printer)) { + return new FormatDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT)); + } else { + final DateTimeFormatter[] parsersWithDefaulting = new DateTimeFormatter[parsers.length]; + for (int i = 0; i < parsers.length; i++) { + DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(parsers[i]); + fields.forEach(builder::parseDefaulting); + parsersWithDefaulting[i] = builder.toFormatter(Locale.ROOT); + } + return new FormatDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT), parsersWithDefaulting); + } + } } diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java index 5793bcf8a0e49..d72f3b3725b59 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java @@ -21,7 +21,7 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; @@ -64,9 +64,9 @@ public class XContentElasticsearchExtension implements XContentBuilderExtension { public static final DateTimeFormatter DEFAULT_DATE_PRINTER = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); - public static final CompoundDateTimeFormatter DEFAULT_FORMATTER = DateFormatters.forPattern("strict_date_optional_time_nanos"); - public static final CompoundDateTimeFormatter LOCAL_TIME_FORMATTER = DateFormatters.forPattern("HH:mm:ss.SSS"); - public static final CompoundDateTimeFormatter OFFSET_TIME_FORMATTER = DateFormatters.forPattern("HH:mm:ss.SSSZZZZZ"); + public static final DateFormatter DEFAULT_FORMATTER = DateFormatters.forPattern("strict_date_optional_time_nanos"); + public static final DateFormatter LOCAL_TIME_FORMATTER = DateFormatters.forPattern("HH:mm:ss.SSS"); + public static final DateFormatter OFFSET_TIME_FORMATTER = DateFormatters.forPattern("HH:mm:ss.SSSZZZZZ"); @Override public Map, XContentBuilder.Writer> getXContentWriters() { @@ -133,14 +133,14 @@ public Map, Function> getDateTransformers() { transformers.put(Calendar.class, d -> DEFAULT_DATE_PRINTER.print(((Calendar) d).getTimeInMillis())); transformers.put(GregorianCalendar.class, d -> DEFAULT_DATE_PRINTER.print(((Calendar) d).getTimeInMillis())); transformers.put(Instant.class, d -> DEFAULT_DATE_PRINTER.print((Instant) d)); - transformers.put(ZonedDateTime.class, d -> DEFAULT_FORMATTER.format((ZonedDateTime) d)); - transformers.put(OffsetDateTime.class, d -> DEFAULT_FORMATTER.format((OffsetDateTime) d)); - transformers.put(OffsetTime.class, d -> OFFSET_TIME_FORMATTER.format((OffsetTime) d)); - transformers.put(LocalDateTime.class, d -> DEFAULT_FORMATTER.format((LocalDateTime) d)); + transformers.put(ZonedDateTime.class, d -> DEFAULT_FORMATTER.print((ZonedDateTime) d)); + transformers.put(OffsetDateTime.class, d -> DEFAULT_FORMATTER.print((OffsetDateTime) d)); + transformers.put(OffsetTime.class, d -> OFFSET_TIME_FORMATTER.print((OffsetTime) d)); + transformers.put(LocalDateTime.class, d -> DEFAULT_FORMATTER.print((LocalDateTime) d)); transformers.put(java.time.Instant.class, - d -> DEFAULT_FORMATTER.format(ZonedDateTime.ofInstant((java.time.Instant) d, ZoneOffset.UTC))); + d -> DEFAULT_FORMATTER.print(ZonedDateTime.ofInstant((java.time.Instant) d, ZoneOffset.UTC))); transformers.put(LocalDate.class, d -> ((LocalDate) d).toString()); - transformers.put(LocalTime.class, d -> LOCAL_TIME_FORMATTER.format((LocalTime) d)); + transformers.put(LocalTime.class, d -> LOCAL_TIME_FORMATTER.print((LocalTime) d)); return transformers; } } diff --git a/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java b/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java index 10a3e81163ab6..73f7ef7882507 100644 --- a/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java +++ b/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java @@ -21,7 +21,7 @@ import org.apache.lucene.util.CollectionUtil; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.unit.TimeValue; @@ -43,7 +43,7 @@ public class HotThreads { private static final Object mutex = new Object(); - private static final CompoundDateTimeFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("dateOptionalTime"); + private static final DateFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("dateOptionalTime"); private int busiestThreads = 3; private TimeValue interval = new TimeValue(500, TimeUnit.MILLISECONDS); @@ -138,7 +138,7 @@ private String innerDetect() throws Exception { StringBuilder sb = new StringBuilder(); sb.append("Hot threads at "); - sb.append(DATE_TIME_FORMATTER.format(LocalDateTime.now(Clock.systemUTC()))); + sb.append(DATE_TIME_FORMATTER.print(LocalDateTime.now(Clock.systemUTC()))); sb.append(", interval="); sb.append(interval); sb.append(", busiestThreads="); diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java index 1a859933ad3fe..7586a44de2e76 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java @@ -382,7 +382,7 @@ Table buildTable(RestRequest request, Index[] indices, ClusterHealthResponse res table.addCell(indexMetaData.getCreationDate()); ZonedDateTime creationTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(indexMetaData.getCreationDate()), ZoneOffset.UTC); - table.addCell(DateFormatters.forPattern("strict_date_time").format(creationTime)); + table.addCell(DateFormatters.forPattern("strict_date_time").print(creationTime)); table.addCell(totalStats.getStore() == null ? null : totalStats.getStore().size()); table.addCell(primaryStats.getStore() == null ? null : primaryStats.getStore().size()); diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java index a03fa2c059eea..1a64e46773c84 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java @@ -232,7 +232,7 @@ private Table buildTable(RestRequest request, ClusterStateResponse state, Indice if (shard.unassignedInfo() != null) { table.addCell(shard.unassignedInfo().getReason()); Instant unassignedTime = Instant.ofEpochMilli(shard.unassignedInfo().getUnassignedTimeInMillis()); - table.addCell(UnassignedInfo.DATE_TIME_FORMATTER.format(unassignedTime)); + table.addCell(UnassignedInfo.DATE_TIME_FORMATTER.print(unassignedTime)); table.addCell(TimeValue.timeValueMillis(System.currentTimeMillis() - shard.unassignedInfo().getUnassignedTimeInMillis())); table.addCell(shard.unassignedInfo().getDetails()); } else { diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java index 2da5e432ca32a..e14468f57355c 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java @@ -25,7 +25,7 @@ import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Table; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.rest.RestController; @@ -99,7 +99,7 @@ protected Table getTableWithHeader(RestRequest request) { .endHeaders(); } - private static final CompoundDateTimeFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC); + private static final DateFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC); private Table buildTable(RestRequest req, GetSnapshotsResponse getSnapshotsResponse) { Table table = getTableWithHeader(req); @@ -109,9 +109,9 @@ private Table buildTable(RestRequest req, GetSnapshotsResponse getSnapshotsRespo table.addCell(snapshotStatus.snapshotId().getName()); table.addCell(snapshotStatus.state()); table.addCell(TimeUnit.SECONDS.convert(snapshotStatus.startTime(), TimeUnit.MILLISECONDS)); - table.addCell(FORMATTER.format(Instant.ofEpochMilli(snapshotStatus.startTime()))); + table.addCell(FORMATTER.print(Instant.ofEpochMilli(snapshotStatus.startTime()))); table.addCell(TimeUnit.SECONDS.convert(snapshotStatus.endTime(), TimeUnit.MILLISECONDS)); - table.addCell(FORMATTER.format(Instant.ofEpochMilli(snapshotStatus.endTime()))); + table.addCell(FORMATTER.print(Instant.ofEpochMilli(snapshotStatus.endTime()))); final long durationMillis; if (snapshotStatus.state() == SnapshotState.IN_PROGRESS) { durationMillis = System.currentTimeMillis() - snapshotStatus.startTime(); diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java index 7d14422b37c2b..f4b7674b52966 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java @@ -27,7 +27,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.Table; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.rest.RestController; @@ -125,7 +125,7 @@ protected Table getTableWithHeader(final RestRequest request) { return table; } - private static final CompoundDateTimeFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC); + private static final DateFormatter FORMATTER = DateFormatters.forPattern("HH:mm:ss").withZone(ZoneOffset.UTC); private void buildRow(Table table, boolean fullId, boolean detailed, DiscoveryNodes discoveryNodes, TaskInfo taskInfo) { table.startRow(); @@ -142,7 +142,7 @@ private void buildRow(Table table, boolean fullId, boolean detailed, DiscoveryNo } table.addCell(taskInfo.getType()); table.addCell(taskInfo.getStartTime()); - table.addCell(FORMATTER.format(Instant.ofEpochMilli(taskInfo.getStartTime()))); + table.addCell(FORMATTER.print(Instant.ofEpochMilli(taskInfo.getStartTime()))); table.addCell(taskInfo.getRunningTimeNanos()); table.addCell(TimeValue.timeValueNanos(taskInfo.getRunningTimeNanos()).toString()); diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java index fdbe74d8d4dd9..6bfaa62f86c27 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java @@ -27,7 +27,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ObjectParser; @@ -52,7 +52,7 @@ public final class SnapshotInfo implements Comparable, ToXContent, public static final String CONTEXT_MODE_PARAM = "context_mode"; public static final String CONTEXT_MODE_SNAPSHOT = "SNAPSHOT"; - private static final CompoundDateTimeFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("strictDateOptionalTime"); + private static final DateFormatter DATE_TIME_FORMATTER = DateFormatters.forPattern("strictDateOptionalTime"); private static final String SNAPSHOT = "snapshot"; private static final String UUID = "uuid"; private static final String INDICES = "indices"; @@ -497,11 +497,11 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa builder.field(REASON, reason); } if (verbose || startTime != 0) { - builder.field(START_TIME, DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(startTime).atZone(ZoneOffset.UTC))); + builder.field(START_TIME, DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(startTime).atZone(ZoneOffset.UTC))); builder.field(START_TIME_IN_MILLIS, startTime); } if (verbose || endTime != 0) { - builder.field(END_TIME, DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(endTime).atZone(ZoneOffset.UTC))); + builder.field(END_TIME, DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(endTime).atZone(ZoneOffset.UTC))); builder.field(END_TIME_IN_MILLIS, endTime); builder.humanReadableField(DURATION_IN_MILLIS, DURATION, new TimeValue(endTime - startTime)); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java index 7823cd8849cf7..38f729c155375 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java @@ -274,7 +274,7 @@ public void testRolloverOnExistingIndex() throws Exception { public void testRolloverWithDateMath() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - String index = "test-" + DateFormatters.forPattern("YYYY.MM.dd").format(now) + "-1"; + String index = "test-" + DateFormatters.forPattern("YYYY.MM.dd").print(now) + "-1"; String dateMathExp = ""; assertAcked(prepareCreate(dateMathExp).addAlias(new Alias("test_alias")).get()); ensureGreen(index); @@ -288,14 +288,14 @@ public void testRolloverWithDateMath() { ensureGreen(index); RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get(); assertThat(response.getOldIndex(), equalTo(index)); - assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000002")); + assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000002")); assertThat(response.isDryRun(), equalTo(false)); assertThat(response.isRolledOver(), equalTo(true)); assertThat(response.getConditionStatus().size(), equalTo(0)); response = client().admin().indices().prepareRolloverIndex("test_alias").get(); - assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000002")); - assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000003")); + assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000002")); + assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000003")); assertThat(response.isDryRun(), equalTo(false)); assertThat(response.isRolledOver(), equalTo(true)); assertThat(response.getConditionStatus().size(), equalTo(0)); @@ -308,8 +308,8 @@ public void testRolloverWithDateMath() { IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); response = client().admin().indices().prepareRolloverIndex("test_alias").setNewIndexName("").get(); - assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000003")); - assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM.dd").format(now) + "-000004")); + assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000003")); + assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM.dd").print(now) + "-000004")); assertThat(response.isDryRun(), equalTo(false)); assertThat(response.isRolledOver(), equalTo(true)); assertThat(response.getConditionStatus().size(), equalTo(0)); diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index cea83bfbccfdc..cdb416ffa58dc 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -19,7 +19,7 @@ package org.elasticsearch.common.joda; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.test.ESTestCase; import org.joda.time.DateTime; @@ -474,7 +474,7 @@ public void testSeveralTimeFormats() { private void assertSamePrinterOutput(String format, ZonedDateTime javaDate, DateTime jodaDate) { assertThat(jodaDate.getMillis(), is(javaDate.toInstant().toEpochMilli())); - String javaTimeOut = DateFormatters.forPattern(format).format(javaDate); + String javaTimeOut = DateFormatters.forPattern(format).print(javaDate); String jodaTimeOut = Joda.forPattern(format).printer().print(jodaDate); String message = String.format(Locale.ROOT, "expected string representation to be equal for format [%s]: joda [%s], java [%s]", format, jodaTimeOut, javaTimeOut); @@ -485,7 +485,7 @@ private void assertSameDate(String input, String format) { FormatDateTimeFormatter jodaFormatter = Joda.forPattern(format); DateTime jodaDateTime = jodaFormatter.parser().parseDateTime(input); - CompoundDateTimeFormatter javaTimeFormatter = DateFormatters.forPattern(format); + DateFormatter javaTimeFormatter = DateFormatters.forPattern(format); TemporalAccessor javaTimeAccessor = javaTimeFormatter.parse(input); ZonedDateTime zonedDateTime = DateFormatters.toZonedDateTime(javaTimeAccessor); @@ -507,7 +507,7 @@ private void assertJodaParseException(String input, String format, String expect } private void assertJavaTimeParseException(String input, String format, String expectedMessage) { - CompoundDateTimeFormatter javaTimeFormatter = DateFormatters.forPattern(format); + DateFormatter javaTimeFormatter = DateFormatters.forPattern(format); DateTimeParseException dateTimeParseException = expectThrows(DateTimeParseException.class, () -> javaTimeFormatter.parse(input)); assertThat(dateTimeParseException.getMessage(), startsWith(expectedMessage)); } diff --git a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java index f96674cf7a4a9..dc51b0a8bc793 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java @@ -33,15 +33,14 @@ public class DateFormattersTests extends ESTestCase { // the epoch milli parser is a bit special, as it does not use date formatter, see comments in DateFormatters public void testEpochMilliParser() { - CompoundDateTimeFormatter formatter = DateFormatters.forPattern("epoch_millis"); + DateFormatter formatter = DateFormatters.forPattern("epoch_millis"); DateTimeParseException e = expectThrows(DateTimeParseException.class, () -> formatter.parse("invalid")); assertThat(e.getMessage(), containsString("invalid number")); - // different zone, should still yield the same output, as epoch is time zoned independent + // different zone, should still yield the same output, as epoch is time zone independent ZoneId zoneId = randomZone(); - CompoundDateTimeFormatter zonedFormatter = formatter.withZone(zoneId); - assertThat(zonedFormatter.printer.getZone(), is(zoneId)); + DateFormatter zonedFormatter = formatter.withZone(zoneId); // test with negative and non negative values assertThatSameDateTime(formatter, zonedFormatter, randomNonNegativeLong() * -1); @@ -58,16 +57,23 @@ public void testEpochMilliParser() { assertSameFormat(formatter, 1); } - private void assertThatSameDateTime(CompoundDateTimeFormatter formatter, CompoundDateTimeFormatter zonedFormatter, long millis) { + public void testEpochMilliParsersWithDifferentFormatters() { + DateFormatter formatter = DateFormatters.forPattern("strict_date_optional_time||epoch_millis"); + TemporalAccessor accessor = formatter.parse("123"); + assertThat(DateFormatters.toZonedDateTime(accessor).toInstant().toEpochMilli(), is(123L)); + assertThat(formatter.format(), is("strict_date_optional_time||epoch_millis")); + } + + private void assertThatSameDateTime(DateFormatter formatter, DateFormatter zonedFormatter, long millis) { String millisAsString = String.valueOf(millis); ZonedDateTime formatterZonedDateTime = DateFormatters.toZonedDateTime(formatter.parse(millisAsString)); ZonedDateTime zonedFormatterZonedDateTime = DateFormatters.toZonedDateTime(zonedFormatter.parse(millisAsString)); assertThat(formatterZonedDateTime.toInstant().toEpochMilli(), is(zonedFormatterZonedDateTime.toInstant().toEpochMilli())); } - private void assertSameFormat(CompoundDateTimeFormatter formatter, long millis) { + private void assertSameFormat(DateFormatter formatter, long millis) { String millisAsString = String.valueOf(millis); TemporalAccessor accessor = formatter.parse(millisAsString); - assertThat(millisAsString, is(formatter.format(accessor))); + assertThat(millisAsString, is(formatter.print(accessor))); } } diff --git a/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java b/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java index d6dc7bb36f2a6..176704442c73d 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java @@ -35,7 +35,7 @@ public class DateMathParserTests extends ESTestCase { - private final CompoundDateTimeFormatter formatter = DateFormatters.forPattern("dateOptionalTime||epoch_millis"); + private final DateFormatter formatter = DateFormatters.forPattern("dateOptionalTime||epoch_millis"); private final DateMathParser parser = new DateMathParser(formatter); public void testBasicDates() { @@ -138,7 +138,7 @@ public void testNow() { public void testRoundingPreservesEpochAsBaseDate() { // If a user only specifies times, then the date needs to always be 1970-01-01 regardless of rounding - CompoundDateTimeFormatter formatter = DateFormatters.forPattern("HH:mm:ss"); + DateFormatter formatter = DateFormatters.forPattern("HH:mm:ss"); DateMathParser parser = new DateMathParser(formatter); ZonedDateTime zonedDateTime = DateFormatters.toZonedDateTime(formatter.parse("04:52:20")); assertThat(zonedDateTime.getYear(), is(1970)); @@ -164,7 +164,7 @@ public void testImplicitRounding() { assertDateMathEquals("2014-11-18T09:20", "2014-11-18T08:20:59.999Z", 0, true, ZoneId.of("CET")); // implicit rounding with explicit timezone in the date format - CompoundDateTimeFormatter formatter = DateFormatters.forPattern("yyyy-MM-ddXXX"); + DateFormatter formatter = DateFormatters.forPattern("yyyy-MM-ddXXX"); DateMathParser parser = new DateMathParser(formatter); long time = parser.parse("2011-10-09+01:00", () -> 0, false, null); assertEquals(this.parser.parse("2011-10-09T00:00:00.000+01:00", () -> 0), time); @@ -300,7 +300,7 @@ private void assertDateEquals(long gotMillis, String original, String expected) ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(gotMillis), ZoneOffset.UTC); fail("Date math not equal\n" + "Original : " + original + "\n" + - "Parsed : " + formatter.format(zonedDateTime) + "\n" + + "Parsed : " + formatter.print(zonedDateTime) + "\n" + "Expected : " + expected + "\n" + "Expected milliseconds : " + expectedMillis + "\n" + "Actual milliseconds : " + gotMillis + "\n"); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java index eeb6e12161383..05af24baf6d45 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java @@ -124,7 +124,7 @@ public void setupSuiteScopeCluster() throws Exception { double doubleTerm = longTerm * Math.PI; ZonedDateTime time = ZonedDateTime.of(2014, 1, ((int) longTerm % 20) + 1, 0, 0, 0, 0, ZoneOffset.UTC); - String dateTerm = DateFormatters.forPattern("yyyy-MM-dd").format(time); + String dateTerm = DateFormatters.forPattern("yyyy-MM-dd").print(time); final int frequency = randomBoolean() ? 1 : randomIntBetween(2, 20); for (int j = 0; j < frequency; ++j) { indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder() diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java index cbc3424314ab3..ecd8868aabd02 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java @@ -26,7 +26,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; -import org.elasticsearch.common.time.CompoundDateTimeFormatter; +import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.aggregations.BaseAggregationTestCase; @@ -137,7 +137,7 @@ private static Document documentForDate(String field, long millis) { } public void testRewriteTimeZone() throws IOException { - CompoundDateTimeFormatter format = DateFormatters.forPattern("strict_date_optional_time"); + DateFormatter format = DateFormatters.forPattern("strict_date_optional_time"); try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) { diff --git a/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java b/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java index 45b6340ba6f46..0d088d5b1a9af 100644 --- a/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java +++ b/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java @@ -553,7 +553,7 @@ public void testStoredFieldsWithoutSource() throws Exception { .field("long_field", 4L) .field("float_field", 5.0f) .field("double_field", 6.0d) - .field("date_field", DateFormatters.forPattern("dateOptionalTime").format(date)) + .field("date_field", DateFormatters.forPattern("dateOptionalTime").print(date)) .field("boolean_field", true) .field("binary_field", Base64.getEncoder().encodeToString("testing text".getBytes("UTF-8"))) .endObject()).execute().actionGet(); @@ -585,7 +585,7 @@ public void testStoredFieldsWithoutSource() throws Exception { assertThat(searchHit.getFields().get("long_field").getValue(), equalTo((Object) 4L)); assertThat(searchHit.getFields().get("float_field").getValue(), equalTo((Object) 5.0f)); assertThat(searchHit.getFields().get("double_field").getValue(), equalTo((Object) 6.0d)); - String dateTime = DateFormatters.forPattern("dateOptionalTime").format(date); + String dateTime = DateFormatters.forPattern("dateOptionalTime").print(date); assertThat(searchHit.getFields().get("date_field").getValue(), equalTo((Object) dateTime)); assertThat(searchHit.getFields().get("boolean_field").getValue(), equalTo((Object) Boolean.TRUE)); assertThat(searchHit.getFields().get("binary_field").getValue(), equalTo(new BytesArray("testing text" .getBytes("UTF8")))); @@ -765,7 +765,7 @@ public void testDocValueFields() throws Exception { .field("long_field", 4L) .field("float_field", 5.0f) .field("double_field", 6.0d) - .field("date_field", DateFormatters.forPattern("dateOptionalTime").format(date)) + .field("date_field", DateFormatters.forPattern("dateOptionalTime").print(date)) .field("boolean_field", true) .field("binary_field", new byte[] {42, 100}) .field("ip_field", "::1") @@ -865,7 +865,7 @@ public void testDocValueFields() throws Exception { assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo((Object) 5.0)); assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo((Object) 6.0d)); assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(), - equalTo(DateFormatters.forPattern("dateOptionalTime").format(date))); + equalTo(DateFormatters.forPattern("dateOptionalTime").print(date))); assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true)); assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo")); assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo")); @@ -895,7 +895,7 @@ public void testDocValueFields() throws Exception { assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo("5.0")); assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo("6.0")); assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(), - equalTo(DateFormatters.forPattern("epoch_millis").format(date))); + equalTo(DateFormatters.forPattern("epoch_millis").print(date))); } public void testScriptFields() throws Exception { From 1208deb3133206672b1f92741683c63af2702ba2 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Thu, 6 Sep 2018 15:44:17 +0200 Subject: [PATCH 2/5] add javadoc --- .../common/time/DateFormatter.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java index e11da89edf912..258ddf2df9371 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java @@ -29,16 +29,56 @@ public interface DateFormatter { + /** + * Try to parse input to a java time TemporalAccessor + * @param input An arbitrary string resembling the string representation of a date or time + * @throws DateTimeParseException If parsing fails, this exception will be thrown. + * Note that it can contained suppressed exceptions when several formatters failed parse this value + * @return The java time object containing the parsed input + */ TemporalAccessor parse(String input); + /** + * Create a copy of this formatter that is configured to parse dates in the specified time zone + * + * @param zoneId The time zone to act on + * @return A copy of the date formatter this has been called on + */ DateFormatter withZone(ZoneId zoneId); + /** + * Print the supplied java time accessor in a string based representation according to this formatter + * + * @param accessor The temporal accessor used to format + * @return The string result for the formatting + */ String print(TemporalAccessor accessor); + /** + * A name based format for this formatter. Can be one of the registered formatters like epoch_millis or + * a configured format like HH:mm:ss + * + * @return The name of this formatter + */ String format(); + /** + * Configure a formatter using default fields for a TemporalAccessor that should be used in case + * the supplied date is not having all of those fields + * + * @param fields A Map<TemporalField, Long> of fields to be used as fallbacks + * @return A new date formatter instance, that will use those fields during parsing + */ DateFormatter parseDefaulting(Map fields); + /** + * Merge several date formatters into a single one. Useful if you need to have several formatters with + * different formats act as one, for example when you specify a + * format like date_hour||epoch_millis + * + * @param formatters The list of date formatters to be merged together + * @return The new date formtter containing the specified date formatters + */ static DateFormatter merge(DateFormatter ... formatters) { return new MergedDateFormatter(formatters); } From cf1613b988dc7e971a3749df2f15f599d97112ac Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Sat, 8 Sep 2018 00:43:52 +0200 Subject: [PATCH 3/5] reduce visibilities --- .../elasticsearch/common/time/EpochMillisDateFormatter.java | 2 +- .../org/elasticsearch/common/time/FormatDateFormatter.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java index f6a6bd986d43d..0d68dae527856 100644 --- a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java @@ -27,7 +27,7 @@ import java.time.temporal.TemporalField; import java.util.Map; -public class EpochMillisDateFormatter implements DateFormatter { +class EpochMillisDateFormatter implements DateFormatter { @Override public TemporalAccessor parse(String input) { diff --git a/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java index 0809a04c193b4..e9bcda7ad703d 100644 --- a/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java @@ -29,13 +29,13 @@ import java.util.Locale; import java.util.Map; -public class FormatDateFormatter implements DateFormatter { +class FormatDateFormatter implements DateFormatter { private final String format; private final DateTimeFormatter printer; private final DateTimeFormatter[] parsers; - public FormatDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { + FormatDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { long distinctZones = Arrays.stream(parsers).map(DateTimeFormatter::getZone).distinct().count(); if (distinctZones > 1) { throw new IllegalArgumentException("formatters must have the same time zone"); From b05fdcea9220bd7ff2ac77439b27663d64f7fe62 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Sat, 8 Sep 2018 13:39:19 +0200 Subject: [PATCH 4/5] incorporate review comments --- .../common/time/DateFormatter.java | 12 +- .../common/time/DateFormatters.java | 150 +++++++++--------- .../common/time/EpochMillisDateFormatter.java | 13 +- ...eFormatter.java => JavaDateFormatter.java} | 14 +- .../common/time/DateFormattersTests.java | 2 +- 5 files changed, 100 insertions(+), 91 deletions(-) rename server/src/main/java/org/elasticsearch/common/time/{FormatDateFormatter.java => JavaDateFormatter.java} (86%) diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java index 258ddf2df9371..d16662b23b930 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java @@ -52,7 +52,7 @@ public interface DateFormatter { * @param accessor The temporal accessor used to format * @return The string result for the formatting */ - String print(TemporalAccessor accessor); + String format(TemporalAccessor accessor); /** * A name based format for this formatter. Can be one of the registered formatters like epoch_millis or @@ -60,7 +60,7 @@ public interface DateFormatter { * * @return The name of this formatter */ - String format(); + String pattern(); /** * Configure a formatter using default fields for a TemporalAccessor that should be used in case @@ -90,7 +90,7 @@ class MergedDateFormatter implements DateFormatter { MergedDateFormatter(DateFormatter ... formatters) { this.formatters = formatters; - this.format = Arrays.stream(formatters).map(DateFormatter::format).collect(Collectors.joining("||")); + this.format = Arrays.stream(formatters).map(DateFormatter::pattern).collect(Collectors.joining("||")); } @Override @@ -116,12 +116,12 @@ public DateFormatter withZone(ZoneId zoneId) { } @Override - public String print(TemporalAccessor accessor) { - return formatters[0].print(accessor); + public String format(TemporalAccessor accessor) { + return formatters[0].format(accessor); } @Override - public String format() { + public String pattern() { return format; } diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index 681abf238179d..d0c363f26bece 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -102,7 +102,7 @@ public class DateFormatters { * Returns a generic ISO datetime parser where the date is mandatory and the time is optional. */ private static final DateFormatter STRICT_DATE_OPTIONAL_TIME = - new FormatDateFormatter("strict_date_optional_time", STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, + new JavaDateFormatter("strict_date_optional_time", STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_2); private static final DateTimeFormatter STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1 = new DateTimeFormatterBuilder() @@ -136,7 +136,7 @@ public class DateFormatters { /** * Returns a generic ISO datetime parser where the date is mandatory and the time is optional with nanosecond resolution. */ - private static final DateFormatter STRICT_DATE_OPTIONAL_TIME_NANOS = new FormatDateFormatter("strict_date_optional_time_nanos", + private static final DateFormatter STRICT_DATE_OPTIONAL_TIME_NANOS = new JavaDateFormatter("strict_date_optional_time_nanos", STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_1, STRICT_DATE_OPTIONAL_TIME_FORMATTER_WITH_NANOS_2); @@ -159,7 +159,7 @@ public class DateFormatters { * Returns a basic formatter for a two digit hour of day, two digit minute * of hour, two digit second of minute, and time zone offset (HHmmssZ). */ - private static final DateFormatter BASIC_TIME_NO_MILLIS = new FormatDateFormatter("basic_time_no_millis", + private static final DateFormatter BASIC_TIME_NO_MILLIS = new JavaDateFormatter("basic_time_no_millis", new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -184,7 +184,7 @@ public class DateFormatters { * of hour, two digit second of minute, three digit millis, and time zone * offset (HHmmss.SSSZ). */ - private static final DateFormatter BASIC_TIME = new FormatDateFormatter("basic_time", + private static final DateFormatter BASIC_TIME = new JavaDateFormatter("basic_time", new DateTimeFormatterBuilder().append(BASIC_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -201,7 +201,7 @@ public class DateFormatters { * of hour, two digit second of minute, three digit millis, and time zone * offset prefixed by 'T' ('T'HHmmss.SSSZ). */ - private static final DateFormatter BASIC_T_TIME = new FormatDateFormatter("basic_t_time", + private static final DateFormatter BASIC_T_TIME = new JavaDateFormatter("basic_t_time", new DateTimeFormatterBuilder().append(BASIC_T_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_T_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -212,7 +212,7 @@ public class DateFormatters { * of hour, two digit second of minute, and time zone offset prefixed by 'T' * ('T'HHmmssZ). */ - private static final DateFormatter BASIC_T_TIME_NO_MILLIS = new FormatDateFormatter("basic_t_time_no_millis", + private static final DateFormatter BASIC_T_TIME_NO_MILLIS = new JavaDateFormatter("basic_t_time_no_millis", new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON) @@ -239,7 +239,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic date and time, separated * by a 'T' (yyyyMMdd'T'HHmmss.SSSZ). */ - private static final DateFormatter BASIC_DATE_TIME = new FormatDateFormatter("basic_date_time", + private static final DateFormatter BASIC_DATE_TIME = new JavaDateFormatter("basic_date_time", new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -252,7 +252,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic date and time without millis, * separated by a 'T' (yyyyMMdd'T'HHmmssZ). */ - private static final DateFormatter BASIC_DATE_TIME_NO_MILLIS = new FormatDateFormatter("basic_t_time_no_millis", + private static final DateFormatter BASIC_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_t_time_no_millis", new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(BASIC_DATE_T).append(BASIC_TIME_NO_MILLIS_BASE) @@ -265,14 +265,14 @@ public class DateFormatters { * Returns a formatter for a full ordinal date, using a four * digit year and three digit dayOfYear (yyyyDDD). */ - private static final DateFormatter BASIC_ORDINAL_DATE = new FormatDateFormatter("basic_ordinal_date", + private static final DateFormatter BASIC_ORDINAL_DATE = new JavaDateFormatter("basic_ordinal_date", DateTimeFormatter.ofPattern("yyyyDDD", Locale.ROOT)); /* * Returns a formatter for a full ordinal date and time, using a four * digit year and three digit dayOfYear (yyyyDDD'T'HHmmss.SSSZ). */ - private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new FormatDateFormatter("basic_ordinal_date_time", + private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new JavaDateFormatter("basic_ordinal_date_time", new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER) @@ -284,7 +284,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time without millis, * using a four digit year and three digit dayOfYear (yyyyDDD'T'HHmmssZ). */ - private static final DateFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new FormatDateFormatter("basic_ordinal_date_time_no_millis", + private static final DateFormatter BASIC_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_ordinal_date_time_no_millis", new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendPattern("yyyyDDD").appendLiteral("T").append(BASIC_TIME_NO_MILLIS_BASE) @@ -332,13 +332,13 @@ public class DateFormatters { * digit week of weekyear, and one digit day of week (xxxx'W'wwe). */ private static final DateFormatter STRICT_BASIC_WEEK_DATE = - new FormatDateFormatter("strict_basic_week_date", STRICT_BASIC_WEEK_DATE_PRINTER, STRICT_BASIC_WEEK_DATE_FORMATTER); + new JavaDateFormatter("strict_basic_week_date", STRICT_BASIC_WEEK_DATE_PRINTER, STRICT_BASIC_WEEK_DATE_FORMATTER); /* * Returns a basic formatter that combines a basic weekyear date and time * without millis, separated by a 'T' (xxxx'W'wwe'T'HHmmssX). */ - private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_basic_week_date_no_millis", + private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_basic_week_date_no_millis", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER).append(DateTimeFormatter.ofPattern("'T'HHmmssX", Locale.ROOT)) .toFormatter(Locale.ROOT), @@ -351,7 +351,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic weekyear date and time, * separated by a 'T' (xxxx'W'wwe'T'HHmmss.SSSX). */ - private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME = new FormatDateFormatter("strict_basic_week_date_time", + private static final DateFormatter STRICT_BASIC_WEEK_DATE_TIME = new JavaDateFormatter("strict_basic_week_date_time", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER) .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", Locale.ROOT)) @@ -365,31 +365,31 @@ public class DateFormatters { /* * An ISO date formatter that formats or parses a date without an offset, such as '2011-12-03'. */ - private static final DateFormatter STRICT_DATE = new FormatDateFormatter("strict_date", + private static final DateFormatter STRICT_DATE = new JavaDateFormatter("strict_date", DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT)); /* * A date formatter that formats or parses a date plus an hour without an offset, such as '2011-12-03T01'. */ - private static final DateFormatter STRICT_DATE_HOUR = new FormatDateFormatter("strict_date_hour", + private static final DateFormatter STRICT_DATE_HOUR = new JavaDateFormatter("strict_date_hour", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", Locale.ROOT)); /* * A date formatter that formats or parses a date plus an hour/minute without an offset, such as '2011-12-03T01:10'. */ - private static final DateFormatter STRICT_DATE_HOUR_MINUTE = new FormatDateFormatter("strict_date_hour_minute", + private static final DateFormatter STRICT_DATE_HOUR_MINUTE = new JavaDateFormatter("strict_date_hour_minute", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.ROOT)); /* * A strict date formatter that formats or parses a date without an offset, such as '2011-12-03'. */ private static final DateFormatter STRICT_YEAR_MONTH_DAY = - new FormatDateFormatter("strict_year_month_day", STRICT_YEAR_MONTH_DAY_FORMATTER); + new JavaDateFormatter("strict_year_month_day", STRICT_YEAR_MONTH_DAY_FORMATTER); /* * A strict formatter that formats or parses a year and a month, such as '2011-12'. */ - private static final DateFormatter STRICT_YEAR_MONTH = new FormatDateFormatter("strict_year_month", + private static final DateFormatter STRICT_YEAR_MONTH = new JavaDateFormatter("strict_year_month", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral("-") @@ -399,7 +399,7 @@ public class DateFormatters { /* * A strict formatter that formats or parses a year, such as '2011'. */ - private static final DateFormatter STRICT_YEAR = new FormatDateFormatter("strict_year", new DateTimeFormatterBuilder() + private static final DateFormatter STRICT_YEAR = new JavaDateFormatter("strict_year", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .toFormatter(Locale.ROOT)); @@ -407,7 +407,7 @@ public class DateFormatters { * A strict formatter that formats or parses a hour, minute and second, such as '09:43:25'. */ private static final DateFormatter STRICT_HOUR_MINUTE_SECOND = - new FormatDateFormatter("strict_hour_minute_second", STRICT_HOUR_MINUTE_SECOND_FORMATTER); + new JavaDateFormatter("strict_hour_minute_second", STRICT_HOUR_MINUTE_SECOND_FORMATTER); private static final DateTimeFormatter STRICT_DATE_FORMATTER = new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -422,7 +422,7 @@ public class DateFormatters { * Returns a formatter that combines a full date and time, separated by a 'T' * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter STRICT_DATE_TIME = new FormatDateFormatter("strict_date_time", + private static final DateFormatter STRICT_DATE_TIME = new JavaDateFormatter("strict_date_time", new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -440,7 +440,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time without millis, * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). */ - private static final DateFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_ordinal_date_time_no_millis", + private static final DateFormatter STRICT_ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -457,7 +457,7 @@ public class DateFormatters { * Returns a formatter that combines a full date and time without millis, * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ). */ - private static final DateFormatter STRICT_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_date_time_no_millis", + private static final DateFormatter STRICT_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_DATE_TIME_NO_MILLIS_FORMATTER) @@ -486,7 +486,7 @@ public class DateFormatters { * even though it's named like this */ private static final DateFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS = - new FormatDateFormatter("strict_hour_minute_second_millis", + new JavaDateFormatter("strict_hour_minute_second_millis", STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, STRICT_HOUR_MINUTE_SECOND_MILLIS_FORMATTER); private static final DateFormatter STRICT_HOUR_MINUTE_SECOND_FRACTION = STRICT_HOUR_MINUTE_SECOND_MILLIS; @@ -496,7 +496,7 @@ public class DateFormatters { * two digit minute of hour, two digit second of minute, and three digit * fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). */ - private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION = new FormatDateFormatter( + private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND_FRACTION = new JavaDateFormatter( "strict_date_hour_minute_second_fraction", new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) @@ -518,14 +518,14 @@ public class DateFormatters { * Returns a formatter for a two digit hour of day. (HH) */ private static final DateFormatter STRICT_HOUR = - new FormatDateFormatter("strict_hour", DateTimeFormatter.ofPattern("HH", Locale.ROOT)); + new JavaDateFormatter("strict_hour", DateTimeFormatter.ofPattern("HH", Locale.ROOT)); /* * Returns a formatter for a two digit hour of day and two digit minute of * hour. (HH:mm) */ private static final DateFormatter STRICT_HOUR_MINUTE = - new FormatDateFormatter("strict_hour_minute", DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT)); + new JavaDateFormatter("strict_hour_minute", DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT)); private static final DateTimeFormatter STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) @@ -544,7 +544,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time, using a four * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter STRICT_ORDINAL_DATE_TIME = new FormatDateFormatter("strict_ordinal_date_time", + private static final DateFormatter STRICT_ORDINAL_DATE_TIME = new JavaDateFormatter("strict_ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) @@ -575,7 +575,7 @@ public class DateFormatters { * hour, two digit second of minute, three digit fraction of second, and * time zone offset (HH:mm:ss.SSSZZ). */ - private static final DateFormatter STRICT_TIME = new FormatDateFormatter("strict_time", + private static final DateFormatter STRICT_TIME = new JavaDateFormatter("strict_time", new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_FORMATTER_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -586,7 +586,7 @@ public class DateFormatters { * hour, two digit second of minute, three digit fraction of second, and * time zone offset prefixed by 'T' ('T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter STRICT_T_TIME = new FormatDateFormatter("strict_t_time", + private static final DateFormatter STRICT_T_TIME = new JavaDateFormatter("strict_t_time", new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -606,7 +606,7 @@ public class DateFormatters { * Returns a formatter for a two digit hour of day, two digit minute of * hour, two digit second of minute, and time zone offset (HH:mm:ssZZ). */ - private static final DateFormatter STRICT_TIME_NO_MILLIS = new FormatDateFormatter("strict_time_no_millis", + private static final DateFormatter STRICT_TIME_NO_MILLIS = new JavaDateFormatter("strict_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -617,7 +617,7 @@ public class DateFormatters { * hour, two digit second of minute, and time zone offset prefixed * by 'T' ('T'HH:mm:ssZZ). */ - private static final DateFormatter STRICT_T_TIME_NO_MILLIS = new FormatDateFormatter("strict_t_time_no_millis", + private static final DateFormatter STRICT_T_TIME_NO_MILLIS = new JavaDateFormatter("strict_t_time_no_millis", new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) @@ -644,13 +644,13 @@ public class DateFormatters { * Returns a formatter for a full date as four digit weekyear, two digit * week of weekyear, and one digit day of week (xxxx-'W'ww-e). */ - private static final DateFormatter STRICT_WEEK_DATE = new FormatDateFormatter("strict_week_date", ISO_WEEK_DATE); + private static final DateFormatter STRICT_WEEK_DATE = new JavaDateFormatter("strict_week_date", ISO_WEEK_DATE); /* * Returns a formatter that combines a full weekyear date and time without millis, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ssZZ). */ - private static final DateFormatter STRICT_WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("strict_week_date_time_no_millis", + private static final DateFormatter STRICT_WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("strict_week_date_time_no_millis", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) @@ -663,7 +663,7 @@ public class DateFormatters { * Returns a formatter that combines a full weekyear date and time, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter STRICT_WEEK_DATE_TIME = new FormatDateFormatter("strict_week_date_time", + private static final DateFormatter STRICT_WEEK_DATE_TIME = new JavaDateFormatter("strict_week_date_time", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -674,7 +674,7 @@ public class DateFormatters { /* * Returns a formatter for a four digit weekyear */ - private static final DateFormatter STRICT_WEEKYEAR = new FormatDateFormatter("strict_weekyear", new DateTimeFormatterBuilder() + private static final DateFormatter STRICT_WEEKYEAR = new JavaDateFormatter("strict_weekyear", new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear(), 4, 10, SignStyle.EXCEEDS_PAD) .toFormatter(Locale.ROOT)); @@ -689,13 +689,13 @@ public class DateFormatters { * weekyear. (xxxx-'W'ww) */ private static final DateFormatter STRICT_WEEKYEAR_WEEK = - new FormatDateFormatter("strict_weekyear_week", STRICT_WEEKYEAR_WEEK_FORMATTER); + new JavaDateFormatter("strict_weekyear_week", STRICT_WEEKYEAR_WEEK_FORMATTER); /* * Returns a formatter for a four digit weekyear, two digit week of * weekyear, and one digit day of week. (xxxx-'W'ww-e) */ - private static final DateFormatter STRICT_WEEKYEAR_WEEK_DAY = new FormatDateFormatter("strict_weekyear_week_day", + private static final DateFormatter STRICT_WEEKYEAR_WEEK_DAY = new JavaDateFormatter("strict_weekyear_week_day", new DateTimeFormatterBuilder() .append(STRICT_WEEKYEAR_WEEK_FORMATTER) .appendLiteral("-") @@ -707,14 +707,14 @@ public class DateFormatters { * two digit minute of hour, and two digit second of * minute. (yyyy-MM-dd'T'HH:mm:ss) */ - private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND = new FormatDateFormatter("strict_date_hour_minute_second", + private static final DateFormatter STRICT_DATE_HOUR_MINUTE_SECOND = new JavaDateFormatter("strict_date_hour_minute_second", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT)); /* * A basic formatter for a full date as four digit year, two digit * month of year, and two digit day of month (yyyyMMdd). */ - private static final DateFormatter BASIC_DATE = new FormatDateFormatter("basic_date", + private static final DateFormatter BASIC_DATE = new JavaDateFormatter("basic_date", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 4, SignStyle.NORMAL) .appendValue(MONTH_OF_YEAR, 2, 2, SignStyle.NOT_NEGATIVE) @@ -739,7 +739,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date, using a four * digit year and three digit dayOfYear (yyyy-DDD). */ - private static final DateFormatter STRICT_ORDINAL_DATE = new FormatDateFormatter("strict_ordinal_date", STRICT_ORDINAL_DATE_FORMATTER); + private static final DateFormatter STRICT_ORDINAL_DATE = new JavaDateFormatter("strict_ordinal_date", STRICT_ORDINAL_DATE_FORMATTER); ///////////////////////////////////////// // @@ -775,7 +775,7 @@ public class DateFormatters { * a date formatter with optional time, being very lenient, format is * yyyy-MM-dd'T'HH:mm:ss.SSSZ */ - private static final DateFormatter DATE_OPTIONAL_TIME = new FormatDateFormatter("date_optional_time", + private static final DateFormatter DATE_OPTIONAL_TIME = new JavaDateFormatter("date_optional_time", STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, new DateTimeFormatterBuilder() .append(DATE_FORMATTER) @@ -852,7 +852,7 @@ public class DateFormatters { * digit year and three digit dayOfYear (yyyy-DDD). */ private static final DateFormatter ORDINAL_DATE = - new FormatDateFormatter("ordinal_date", ORDINAL_DATE_PRINTER, ORDINAL_DATE_FORMATTER); + new JavaDateFormatter("ordinal_date", ORDINAL_DATE_PRINTER, ORDINAL_DATE_FORMATTER); private static final DateTimeFormatter TIME_NO_MILLIS_FORMATTER = new DateTimeFormatterBuilder() .appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE) @@ -881,19 +881,19 @@ public class DateFormatters { /* * Returns a formatter for a four digit weekyear. (YYYY) */ - private static final DateFormatter WEEK_YEAR = new FormatDateFormatter("week_year", + private static final DateFormatter WEEK_YEAR = new JavaDateFormatter("week_year", new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(Locale.ROOT)); /* * Returns a formatter for a four digit weekyear. (uuuu) */ - private static final DateFormatter YEAR = new FormatDateFormatter("year", + private static final DateFormatter YEAR = new JavaDateFormatter("year", new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(Locale.ROOT)); /* * Returns a formatter for parsing the seconds since the epoch */ - private static final DateFormatter EPOCH_SECOND = new FormatDateFormatter("epoch_second", + private static final DateFormatter EPOCH_SECOND = new JavaDateFormatter("epoch_second", new DateTimeFormatterBuilder().appendValue(ChronoField.INSTANT_SECONDS).toFormatter(Locale.ROOT)); /* @@ -905,7 +905,7 @@ public class DateFormatters { * Returns a formatter that combines a full date and two digit hour of * day. (yyyy-MM-dd'T'HH) */ - private static final DateFormatter DATE_HOUR = new FormatDateFormatter("date_hour", + private static final DateFormatter DATE_HOUR = new JavaDateFormatter("date_hour", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH", Locale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) @@ -920,7 +920,7 @@ public class DateFormatters { * to 3 fractional second digits. */ private static final DateFormatter DATE_HOUR_MINUTE_SECOND_MILLIS = - new FormatDateFormatter("date_hour_minute_second_millis", + new JavaDateFormatter("date_hour_minute_second_millis", new DateTimeFormatterBuilder() .append(STRICT_YEAR_MONTH_DAY_FORMATTER) .appendLiteral("T") @@ -938,7 +938,7 @@ public class DateFormatters { * Returns a formatter that combines a full date, two digit hour of day, * and two digit minute of hour. (yyyy-MM-dd'T'HH:mm) */ - private static final DateFormatter DATE_HOUR_MINUTE = new FormatDateFormatter("date_hour_minute", + private static final DateFormatter DATE_HOUR_MINUTE = new JavaDateFormatter("date_hour_minute", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm", Locale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) @@ -951,7 +951,7 @@ public class DateFormatters { * two digit minute of hour, and two digit second of * minute. (yyyy-MM-dd'T'HH:mm:ss) */ - private static final DateFormatter DATE_HOUR_MINUTE_SECOND = new FormatDateFormatter("date_hour_minute_second", + private static final DateFormatter DATE_HOUR_MINUTE_SECOND = new JavaDateFormatter("date_hour_minute_second", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT), new DateTimeFormatterBuilder() .append(DATE_FORMATTER) @@ -974,7 +974,7 @@ public class DateFormatters { * Returns a formatter that combines a full date and time, separated by a 'T' * (yyyy-MM-dd'T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter DATE_TIME = new FormatDateFormatter("date_time", + private static final DateFormatter DATE_TIME = new JavaDateFormatter("date_time", STRICT_DATE_OPTIONAL_TIME_FORMATTER_1, new DateTimeFormatterBuilder().append(DATE_TIME_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(DATE_TIME_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -985,13 +985,13 @@ public class DateFormatters { * digit week of weekyear, and one digit day of week (YYYY'W'wwe). */ private static final DateFormatter BASIC_WEEK_DATE = - new FormatDateFormatter("basic_week_date", STRICT_BASIC_WEEK_DATE_PRINTER, BASIC_WEEK_DATE_FORMATTER); + new JavaDateFormatter("basic_week_date", STRICT_BASIC_WEEK_DATE_PRINTER, BASIC_WEEK_DATE_FORMATTER); /* * Returns a formatter for a full date as four digit year, two digit month * of year, and two digit day of month (yyyy-MM-dd). */ - private static final DateFormatter DATE = new FormatDateFormatter("date", + private static final DateFormatter DATE = new JavaDateFormatter("date", DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT), DATE_FORMATTER); @@ -1019,7 +1019,7 @@ public class DateFormatters { * Returns a formatter that combines a full date and time without millis, but with a timezone that can be optional * separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZ). */ - private static final DateFormatter DATE_TIME_NO_MILLIS = new FormatDateFormatter("date_time_no_millis", + private static final DateFormatter DATE_TIME_NO_MILLIS = new JavaDateFormatter("date_time_no_millis", DATE_TIME_NO_MILLIS_PRINTER, new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(DATE_TIME_PREFIX).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT), @@ -1034,7 +1034,7 @@ public class DateFormatters { * hour, two digit second of minute, and three digit fraction of * second (HH:mm:ss.SSS). */ - private static final DateFormatter HOUR_MINUTE_SECOND_MILLIS = new FormatDateFormatter("hour_minute_second_millis", + private static final DateFormatter HOUR_MINUTE_SECOND_MILLIS = new JavaDateFormatter("hour_minute_second_millis", STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, HOUR_MINUTE_SECOND_MILLIS_FORMATTER); /* @@ -1042,12 +1042,12 @@ public class DateFormatters { * hour. (HH:mm) */ private static final DateFormatter HOUR_MINUTE = - new FormatDateFormatter("hour_minute", DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT), HOUR_MINUTE_FORMATTER); + new JavaDateFormatter("hour_minute", DateTimeFormatter.ofPattern("HH:mm", Locale.ROOT), HOUR_MINUTE_FORMATTER); /* * A strict formatter that formats or parses a hour, minute and second, such as '09:43:25'. */ - private static final DateFormatter HOUR_MINUTE_SECOND = new FormatDateFormatter("hour_minute_second", + private static final DateFormatter HOUR_MINUTE_SECOND = new JavaDateFormatter("hour_minute_second", STRICT_HOUR_MINUTE_SECOND_FORMATTER, new DateTimeFormatterBuilder() .append(HOUR_MINUTE_FORMATTER) @@ -1059,7 +1059,7 @@ public class DateFormatters { /* * Returns a formatter for a two digit hour of day. (HH) */ - private static final DateFormatter HOUR = new FormatDateFormatter("hour", + private static final DateFormatter HOUR = new JavaDateFormatter("hour", DateTimeFormatter.ofPattern("HH", Locale.ROOT), new DateTimeFormatterBuilder().appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE).toFormatter(Locale.ROOT) ); @@ -1079,7 +1079,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time, using a four * digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter ORDINAL_DATE_TIME = new FormatDateFormatter("ordinal_date_time", + private static final DateFormatter ORDINAL_DATE_TIME = new JavaDateFormatter("ordinal_date_time", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_FORMATTER_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_FORMATTER_BASE) @@ -1098,7 +1098,7 @@ public class DateFormatters { * Returns a formatter for a full ordinal date and time without millis, * using a four digit year and three digit dayOfYear (yyyy-DDD'T'HH:mm:ssZZ). */ - private static final DateFormatter ORDINAL_DATE_TIME_NO_MILLIS = new FormatDateFormatter("ordinal_date_time_no_millis", + private static final DateFormatter ORDINAL_DATE_TIME_NO_MILLIS = new JavaDateFormatter("ordinal_date_time_no_millis", new DateTimeFormatterBuilder().append(STRICT_ORDINAL_DATE_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(ORDINAL_DATE_TIME_NO_MILLIS_BASE) @@ -1111,7 +1111,7 @@ public class DateFormatters { * Returns a formatter that combines a full weekyear date and time, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter WEEK_DATE_TIME = new FormatDateFormatter("week_date_time", + private static final DateFormatter WEEK_DATE_TIME = new JavaDateFormatter("week_date_time", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T).append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).appendLiteral("T").append(TIME_PREFIX) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -1123,7 +1123,7 @@ public class DateFormatters { * Returns a formatter that combines a full weekyear date and time, * separated by a 'T' (xxxx-'W'ww-e'T'HH:mm:ssZZ). */ - private static final DateFormatter WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("week_date_time_no_millis", + private static final DateFormatter WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("week_date_time_no_millis", new DateTimeFormatterBuilder().append(ISO_WEEK_DATE_T) .append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(WEEK_DATE_FORMATTER).append(T_TIME_NO_MILLIS_FORMATTER) @@ -1136,7 +1136,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic weekyear date and time, * separated by a 'T' (xxxx'W'wwe'T'HHmmss.SSSX). */ - private static final DateFormatter BASIC_WEEK_DATE_TIME = new FormatDateFormatter("basic_week_date_time", + private static final DateFormatter BASIC_WEEK_DATE_TIME = new JavaDateFormatter("basic_week_date_time", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER) .append(DateTimeFormatter.ofPattern("'T'HHmmss.SSSX", Locale.ROOT)) @@ -1151,7 +1151,7 @@ public class DateFormatters { * Returns a basic formatter that combines a basic weekyear date and time, * separated by a 'T' (xxxx'W'wwe'T'HHmmssX). */ - private static final DateFormatter BASIC_WEEK_DATE_TIME_NO_MILLIS = new FormatDateFormatter("basic_week_date_time_no_millis", + private static final DateFormatter BASIC_WEEK_DATE_TIME_NO_MILLIS = new JavaDateFormatter("basic_week_date_time_no_millis", new DateTimeFormatterBuilder() .append(STRICT_BASIC_WEEK_DATE_PRINTER).append(DateTimeFormatter.ofPattern("'T'HHmmssX", Locale.ROOT)) .toFormatter(Locale.ROOT), @@ -1166,7 +1166,7 @@ public class DateFormatters { * hour, two digit second of minute, three digit fraction of second, and * time zone offset (HH:mm:ss.SSSZZ). */ - private static final DateFormatter TIME = new FormatDateFormatter("time", + private static final DateFormatter TIME = new JavaDateFormatter("time", new DateTimeFormatterBuilder().append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_PREFIX).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_PREFIX).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -1176,7 +1176,7 @@ public class DateFormatters { * Returns a formatter for a two digit hour of day, two digit minute of * hour, two digit second of minute, andtime zone offset (HH:mm:ssZZ). */ - private static final DateFormatter TIME_NO_MILLIS = new FormatDateFormatter("time_no_millis", + private static final DateFormatter TIME_NO_MILLIS = new JavaDateFormatter("time_no_millis", new DateTimeFormatterBuilder().append(STRICT_TIME_NO_MILLIS_BASE).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(TIME_NO_MILLIS_FORMATTER).append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT) @@ -1187,7 +1187,7 @@ public class DateFormatters { * hour, two digit second of minute, three digit fraction of second, and * time zone offset prefixed by 'T' ('T'HH:mm:ss.SSSZZ). */ - private static final DateFormatter T_TIME = new FormatDateFormatter("t_time", + private static final DateFormatter T_TIME = new JavaDateFormatter("t_time", new DateTimeFormatterBuilder().appendLiteral('T').append(STRICT_TIME_PRINTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().appendLiteral("T").append(TIME_PREFIX) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -1200,7 +1200,7 @@ public class DateFormatters { * hour, two digit second of minute, and time zone offset prefixed * by 'T' ('T'HH:mm:ssZZ). */ - private static final DateFormatter T_TIME_NO_MILLIS = new FormatDateFormatter("t_time_no_millis", + private static final DateFormatter T_TIME_NO_MILLIS = new JavaDateFormatter("t_time_no_millis", new DateTimeFormatterBuilder().appendLiteral("T").append(STRICT_TIME_NO_MILLIS_BASE) .appendZoneOrOffsetId().toFormatter(Locale.ROOT), new DateTimeFormatterBuilder().append(T_TIME_NO_MILLIS_FORMATTER).appendZoneOrOffsetId().toFormatter(Locale.ROOT), @@ -1210,7 +1210,7 @@ public class DateFormatters { /* * A strict formatter that formats or parses a year and a month, such as '2011-12'. */ - private static final DateFormatter YEAR_MONTH = new FormatDateFormatter("year_month", + private static final DateFormatter YEAR_MONTH = new JavaDateFormatter("year_month", new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD) .appendLiteral("-") @@ -1222,7 +1222,7 @@ public class DateFormatters { /* * A strict date formatter that formats or parses a date without an offset, such as '2011-12-03'. */ - private static final DateFormatter YEAR_MONTH_DAY = new FormatDateFormatter("year_month_day", + private static final DateFormatter YEAR_MONTH_DAY = new JavaDateFormatter("year_month_day", STRICT_YEAR_MONTH_DAY_FORMATTER, new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) @@ -1237,13 +1237,13 @@ public class DateFormatters { * Returns a formatter for a full date as four digit weekyear, two digit * week of weekyear, and one digit day of week (xxxx-'W'ww-e). */ - private static final DateFormatter WEEK_DATE = new FormatDateFormatter("week_date", ISO_WEEK_DATE, WEEK_DATE_FORMATTER); + private static final DateFormatter WEEK_DATE = new JavaDateFormatter("week_date", ISO_WEEK_DATE, WEEK_DATE_FORMATTER); /* * Returns a formatter for a four digit weekyear and two digit week of * weekyear. (xxxx-'W'ww) */ - private static final DateFormatter WEEKYEAR_WEEK = new FormatDateFormatter("weekyear_week", STRICT_WEEKYEAR_WEEK_FORMATTER, + private static final DateFormatter WEEKYEAR_WEEK = new JavaDateFormatter("weekyear_week", STRICT_WEEKYEAR_WEEK_FORMATTER, new DateTimeFormatterBuilder() .appendValue(WeekFields.ISO.weekBasedYear()) .appendLiteral("-W") @@ -1255,7 +1255,7 @@ public class DateFormatters { * Returns a formatter for a four digit weekyear, two digit week of * weekyear, and one digit day of week. (xxxx-'W'ww-e) */ - private static final DateFormatter WEEKYEAR_WEEK_DAY = new FormatDateFormatter("weekyear_week_day", + private static final DateFormatter WEEKYEAR_WEEK_DAY = new JavaDateFormatter("weekyear_week_day", new DateTimeFormatterBuilder() .append(STRICT_WEEKYEAR_WEEK_FORMATTER) .appendLiteral("-") @@ -1465,7 +1465,7 @@ public static DateFormatter forPattern(String input, Locale locale) { } } else { try { - return new FormatDateFormatter(input, new DateTimeFormatterBuilder().appendPattern(input).toFormatter(locale)); + return new JavaDateFormatter(input, new DateTimeFormatterBuilder().appendPattern(input).toFormatter(locale)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid format: [" + input + "]: " + e.getMessage(), e); } diff --git a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java index 0d68dae527856..1e4c863b3d0ae 100644 --- a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java @@ -27,6 +27,15 @@ import java.time.temporal.TemporalField; import java.util.Map; +/** + * This is a special formatter to parse the milliseconds since the epoch. + * There is no way using a native java time date formatter to resemble + * the required behaviour to parse negative milliseconds as well. + * + * This implementation simply tries to convert the input to a long and uses + * this as the milliseconds since the epoch without involving any other + * java time code + */ class EpochMillisDateFormatter implements DateFormatter { @Override @@ -44,12 +53,12 @@ public DateFormatter withZone(ZoneId zoneId) { } @Override - public String print(TemporalAccessor accessor) { + public String format(TemporalAccessor accessor) { return String.valueOf(Instant.from(accessor).toEpochMilli()); } @Override - public String format() { + public String pattern() { return "epoch_millis"; } diff --git a/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java similarity index 86% rename from server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java rename to server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java index e9bcda7ad703d..f68215fde492a 100644 --- a/server/src/main/java/org/elasticsearch/common/time/FormatDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java @@ -29,13 +29,13 @@ import java.util.Locale; import java.util.Map; -class FormatDateFormatter implements DateFormatter { +class JavaDateFormatter implements DateFormatter { private final String format; private final DateTimeFormatter printer; private final DateTimeFormatter[] parsers; - FormatDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { + JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) { long distinctZones = Arrays.stream(parsers).map(DateTimeFormatter::getZone).distinct().count(); if (distinctZones > 1) { throw new IllegalArgumentException("formatters must have the same time zone"); @@ -80,16 +80,16 @@ public DateFormatter withZone(ZoneId zoneId) { parsersWithZone[i] = parsers[i].withZone(zoneId); } - return new FormatDateFormatter(format, printer.withZone(zoneId), parsersWithZone); + return new JavaDateFormatter(format, printer.withZone(zoneId), parsersWithZone); } @Override - public String print(TemporalAccessor accessor) { + public String format(TemporalAccessor accessor) { return printer.format(accessor); } @Override - public String format() { + public String pattern() { return format; } @@ -98,7 +98,7 @@ public DateFormatter parseDefaulting(Map fields) { final DateTimeFormatterBuilder parseDefaultingBuilder = new DateTimeFormatterBuilder().append(printer); fields.forEach(parseDefaultingBuilder::parseDefaulting); if (parsers.length == 1 && parsers[0].equals(printer)) { - return new FormatDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT)); + return new JavaDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT)); } else { final DateTimeFormatter[] parsersWithDefaulting = new DateTimeFormatter[parsers.length]; for (int i = 0; i < parsers.length; i++) { @@ -106,7 +106,7 @@ public DateFormatter parseDefaulting(Map fields) { fields.forEach(builder::parseDefaulting); parsersWithDefaulting[i] = builder.toFormatter(Locale.ROOT); } - return new FormatDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT), parsersWithDefaulting); + return new JavaDateFormatter(format, parseDefaultingBuilder.toFormatter(Locale.ROOT), parsersWithDefaulting); } } } diff --git a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java index dc51b0a8bc793..1dca4b8e4b922 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java @@ -61,7 +61,7 @@ public void testEpochMilliParsersWithDifferentFormatters() { DateFormatter formatter = DateFormatters.forPattern("strict_date_optional_time||epoch_millis"); TemporalAccessor accessor = formatter.parse("123"); assertThat(DateFormatters.toZonedDateTime(accessor).toInstant().toEpochMilli(), is(123L)); - assertThat(formatter.format(), is("strict_date_optional_time||epoch_millis")); + assertThat(formatter.pattern(), is("strict_date_optional_time||epoch_millis")); } private void assertThatSameDateTime(DateFormatter formatter, DateFormatter zonedFormatter, long millis) { From 754c884be3c17836e6992560d717a9c2aed91d24 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Sat, 8 Sep 2018 23:30:50 +0200 Subject: [PATCH 5/5] fix broken refactoring --- .../allocation/ClusterAllocationExplanation.java | 2 +- .../cluster/metadata/IndexGraveyard.java | 2 +- .../cluster/routing/UnassignedInfo.java | 4 ++-- .../main/java/org/elasticsearch/common/Table.java | 2 +- .../elasticsearch/common/time/DateFormatters.java | 2 +- .../common/time/EpochMillisDateFormatter.java | 4 ++++ .../xcontent/XContentElasticsearchExtension.java | 12 ++++++------ .../org/elasticsearch/monitor/jvm/HotThreads.java | 2 +- .../rest/action/cat/RestIndicesAction.java | 2 +- .../rest/action/cat/RestShardsAction.java | 2 +- .../rest/action/cat/RestSnapshotAction.java | 4 ++-- .../rest/action/cat/RestTasksAction.java | 2 +- .../org/elasticsearch/snapshots/SnapshotInfo.java | 4 ++-- .../action/admin/indices/rollover/RolloverIT.java | 12 ++++++------ .../common/joda/JavaJodaTimeDuellingTests.java | 2 +- .../common/time/DateFormattersTests.java | 3 +-- .../common/time/DateMathParserTests.java | 2 +- .../search/aggregations/bucket/MinDocCountIT.java | 2 +- .../elasticsearch/search/fields/SearchFieldsIT.java | 10 +++++----- 19 files changed, 39 insertions(+), 36 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java index d0386e0048710..cdf122545c4e2 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java @@ -191,7 +191,7 @@ private XContentBuilder unassignedInfoToXContent(UnassignedInfo unassignedInfo, builder.startObject("unassigned_info"); builder.field("reason", unassignedInfo.getReason()); builder.field("at", - UnassignedInfo.DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(unassignedInfo.getUnassignedTimeInMillis()))); + UnassignedInfo.DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedInfo.getUnassignedTimeInMillis()))); if (unassignedInfo.getNumFailedAllocations() > 0) { builder.field("failed_allocation_attempts", unassignedInfo.getNumFailedAllocations()); } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java index 5814c89b2fb60..7a43ce31d33df 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexGraveyard.java @@ -433,7 +433,7 @@ public int hashCode() { @Override public String toString() { - String date = FORMATTER.print(Instant.ofEpochMilli(deleteDateInMillis)); + String date = FORMATTER.format(Instant.ofEpochMilli(deleteDateInMillis)); return "[index=" + index + ", deleteDate=" + date + "]"; } diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java b/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java index 89a0ec31ed679..21885d1788c7e 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/UnassignedInfo.java @@ -410,7 +410,7 @@ public static long findNextDelayedAllocation(long currentNanoTime, ClusterState public String shortSummary() { StringBuilder sb = new StringBuilder(); sb.append("[reason=").append(reason).append("]"); - sb.append(", at[").append(DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(unassignedTimeMillis))).append("]"); + sb.append(", at[").append(DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis))).append("]"); if (failedAllocations > 0) { sb.append(", failed_attempts[").append(failedAllocations).append("]"); } @@ -433,7 +433,7 @@ public String toString() { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("unassigned_info"); builder.field("reason", reason); - builder.field("at", DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(unassignedTimeMillis))); + builder.field("at", DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(unassignedTimeMillis))); if (failedAllocations > 0) { builder.field("failed_attempts", failedAllocations); } diff --git a/server/src/main/java/org/elasticsearch/common/Table.java b/server/src/main/java/org/elasticsearch/common/Table.java index 9d0ec25d49e8d..a41fd267329ff 100644 --- a/server/src/main/java/org/elasticsearch/common/Table.java +++ b/server/src/main/java/org/elasticsearch/common/Table.java @@ -95,7 +95,7 @@ public Table startRow() { if (withTime) { long time = System.currentTimeMillis(); addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)); - addCell(FORMATTER.print(Instant.ofEpochMilli(time))); + addCell(FORMATTER.format(Instant.ofEpochMilli(time))); } return this; } diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java index d0c363f26bece..5f68765134498 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatters.java @@ -899,7 +899,7 @@ public class DateFormatters { /* * Parses the milliseconds since/before the epoch */ - private static final DateFormatter EPOCH_MILLIS = new EpochMillisDateFormatter(); + private static final DateFormatter EPOCH_MILLIS = EpochMillisDateFormatter.INSTANCE; /* * Returns a formatter that combines a full date and two digit hour of diff --git a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java index 1e4c863b3d0ae..d50cc0cf466a9 100644 --- a/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/EpochMillisDateFormatter.java @@ -38,6 +38,10 @@ */ class EpochMillisDateFormatter implements DateFormatter { + public static DateFormatter INSTANCE = new EpochMillisDateFormatter(); + + private EpochMillisDateFormatter() {} + @Override public TemporalAccessor parse(String input) { try { diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java index d72f3b3725b59..684e96f678cee 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java @@ -133,14 +133,14 @@ public Map, Function> getDateTransformers() { transformers.put(Calendar.class, d -> DEFAULT_DATE_PRINTER.print(((Calendar) d).getTimeInMillis())); transformers.put(GregorianCalendar.class, d -> DEFAULT_DATE_PRINTER.print(((Calendar) d).getTimeInMillis())); transformers.put(Instant.class, d -> DEFAULT_DATE_PRINTER.print((Instant) d)); - transformers.put(ZonedDateTime.class, d -> DEFAULT_FORMATTER.print((ZonedDateTime) d)); - transformers.put(OffsetDateTime.class, d -> DEFAULT_FORMATTER.print((OffsetDateTime) d)); - transformers.put(OffsetTime.class, d -> OFFSET_TIME_FORMATTER.print((OffsetTime) d)); - transformers.put(LocalDateTime.class, d -> DEFAULT_FORMATTER.print((LocalDateTime) d)); + transformers.put(ZonedDateTime.class, d -> DEFAULT_FORMATTER.format((ZonedDateTime) d)); + transformers.put(OffsetDateTime.class, d -> DEFAULT_FORMATTER.format((OffsetDateTime) d)); + transformers.put(OffsetTime.class, d -> OFFSET_TIME_FORMATTER.format((OffsetTime) d)); + transformers.put(LocalDateTime.class, d -> DEFAULT_FORMATTER.format((LocalDateTime) d)); transformers.put(java.time.Instant.class, - d -> DEFAULT_FORMATTER.print(ZonedDateTime.ofInstant((java.time.Instant) d, ZoneOffset.UTC))); + d -> DEFAULT_FORMATTER.format(ZonedDateTime.ofInstant((java.time.Instant) d, ZoneOffset.UTC))); transformers.put(LocalDate.class, d -> ((LocalDate) d).toString()); - transformers.put(LocalTime.class, d -> LOCAL_TIME_FORMATTER.print((LocalTime) d)); + transformers.put(LocalTime.class, d -> LOCAL_TIME_FORMATTER.format((LocalTime) d)); return transformers; } } diff --git a/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java b/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java index 73f7ef7882507..7e00aaa7cd99c 100644 --- a/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java +++ b/server/src/main/java/org/elasticsearch/monitor/jvm/HotThreads.java @@ -138,7 +138,7 @@ private String innerDetect() throws Exception { StringBuilder sb = new StringBuilder(); sb.append("Hot threads at "); - sb.append(DATE_TIME_FORMATTER.print(LocalDateTime.now(Clock.systemUTC()))); + sb.append(DATE_TIME_FORMATTER.format(LocalDateTime.now(Clock.systemUTC()))); sb.append(", interval="); sb.append(interval); sb.append(", busiestThreads="); diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java index 7586a44de2e76..1a859933ad3fe 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java @@ -382,7 +382,7 @@ Table buildTable(RestRequest request, Index[] indices, ClusterHealthResponse res table.addCell(indexMetaData.getCreationDate()); ZonedDateTime creationTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(indexMetaData.getCreationDate()), ZoneOffset.UTC); - table.addCell(DateFormatters.forPattern("strict_date_time").print(creationTime)); + table.addCell(DateFormatters.forPattern("strict_date_time").format(creationTime)); table.addCell(totalStats.getStore() == null ? null : totalStats.getStore().size()); table.addCell(primaryStats.getStore() == null ? null : primaryStats.getStore().size()); diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java index 1a64e46773c84..a03fa2c059eea 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestShardsAction.java @@ -232,7 +232,7 @@ private Table buildTable(RestRequest request, ClusterStateResponse state, Indice if (shard.unassignedInfo() != null) { table.addCell(shard.unassignedInfo().getReason()); Instant unassignedTime = Instant.ofEpochMilli(shard.unassignedInfo().getUnassignedTimeInMillis()); - table.addCell(UnassignedInfo.DATE_TIME_FORMATTER.print(unassignedTime)); + table.addCell(UnassignedInfo.DATE_TIME_FORMATTER.format(unassignedTime)); table.addCell(TimeValue.timeValueMillis(System.currentTimeMillis() - shard.unassignedInfo().getUnassignedTimeInMillis())); table.addCell(shard.unassignedInfo().getDetails()); } else { diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java index e14468f57355c..fb302b1b3b3a4 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestSnapshotAction.java @@ -109,9 +109,9 @@ private Table buildTable(RestRequest req, GetSnapshotsResponse getSnapshotsRespo table.addCell(snapshotStatus.snapshotId().getName()); table.addCell(snapshotStatus.state()); table.addCell(TimeUnit.SECONDS.convert(snapshotStatus.startTime(), TimeUnit.MILLISECONDS)); - table.addCell(FORMATTER.print(Instant.ofEpochMilli(snapshotStatus.startTime()))); + table.addCell(FORMATTER.format(Instant.ofEpochMilli(snapshotStatus.startTime()))); table.addCell(TimeUnit.SECONDS.convert(snapshotStatus.endTime(), TimeUnit.MILLISECONDS)); - table.addCell(FORMATTER.print(Instant.ofEpochMilli(snapshotStatus.endTime()))); + table.addCell(FORMATTER.format(Instant.ofEpochMilli(snapshotStatus.endTime()))); final long durationMillis; if (snapshotStatus.state() == SnapshotState.IN_PROGRESS) { durationMillis = System.currentTimeMillis() - snapshotStatus.startTime(); diff --git a/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java b/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java index f4b7674b52966..39b3f08dcdc5f 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/cat/RestTasksAction.java @@ -142,7 +142,7 @@ private void buildRow(Table table, boolean fullId, boolean detailed, DiscoveryNo } table.addCell(taskInfo.getType()); table.addCell(taskInfo.getStartTime()); - table.addCell(FORMATTER.print(Instant.ofEpochMilli(taskInfo.getStartTime()))); + table.addCell(FORMATTER.format(Instant.ofEpochMilli(taskInfo.getStartTime()))); table.addCell(taskInfo.getRunningTimeNanos()); table.addCell(TimeValue.timeValueNanos(taskInfo.getRunningTimeNanos()).toString()); diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java index 6bfaa62f86c27..38e31519ea138 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java @@ -497,11 +497,11 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa builder.field(REASON, reason); } if (verbose || startTime != 0) { - builder.field(START_TIME, DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(startTime).atZone(ZoneOffset.UTC))); + builder.field(START_TIME, DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(startTime).atZone(ZoneOffset.UTC))); builder.field(START_TIME_IN_MILLIS, startTime); } if (verbose || endTime != 0) { - builder.field(END_TIME, DATE_TIME_FORMATTER.print(Instant.ofEpochMilli(endTime).atZone(ZoneOffset.UTC))); + builder.field(END_TIME, DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(endTime).atZone(ZoneOffset.UTC))); builder.field(END_TIME_IN_MILLIS, endTime); builder.humanReadableField(DURATION_IN_MILLIS, DURATION, new TimeValue(endTime - startTime)); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java index 38f729c155375..7823cd8849cf7 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java @@ -274,7 +274,7 @@ public void testRolloverOnExistingIndex() throws Exception { public void testRolloverWithDateMath() { ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); - String index = "test-" + DateFormatters.forPattern("YYYY.MM.dd").print(now) + "-1"; + String index = "test-" + DateFormatters.forPattern("YYYY.MM.dd").format(now) + "-1"; String dateMathExp = ""; assertAcked(prepareCreate(dateMathExp).addAlias(new Alias("test_alias")).get()); ensureGreen(index); @@ -288,14 +288,14 @@ public void testRolloverWithDateMath() { ensureGreen(index); RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias").get(); assertThat(response.getOldIndex(), equalTo(index)); - assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000002")); + assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000002")); assertThat(response.isDryRun(), equalTo(false)); assertThat(response.isRolledOver(), equalTo(true)); assertThat(response.getConditionStatus().size(), equalTo(0)); response = client().admin().indices().prepareRolloverIndex("test_alias").get(); - assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000002")); - assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000003")); + assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000002")); + assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000003")); assertThat(response.isDryRun(), equalTo(false)); assertThat(response.isRolledOver(), equalTo(true)); assertThat(response.getConditionStatus().size(), equalTo(0)); @@ -308,8 +308,8 @@ public void testRolloverWithDateMath() { IndexMetaData.SETTING_INDEX_PROVIDED_NAME)); response = client().admin().indices().prepareRolloverIndex("test_alias").setNewIndexName("").get(); - assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").print(now) + "-000003")); - assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM.dd").print(now) + "-000004")); + assertThat(response.getOldIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM").format(now) + "-000003")); + assertThat(response.getNewIndex(), equalTo("test-" + DateFormatters.forPattern("YYYY.MM.dd").format(now) + "-000004")); assertThat(response.isDryRun(), equalTo(false)); assertThat(response.isRolledOver(), equalTo(true)); assertThat(response.getConditionStatus().size(), equalTo(0)); diff --git a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java index cdb416ffa58dc..5203aa07d286e 100644 --- a/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java +++ b/server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java @@ -474,7 +474,7 @@ public void testSeveralTimeFormats() { private void assertSamePrinterOutput(String format, ZonedDateTime javaDate, DateTime jodaDate) { assertThat(jodaDate.getMillis(), is(javaDate.toInstant().toEpochMilli())); - String javaTimeOut = DateFormatters.forPattern(format).print(javaDate); + String javaTimeOut = DateFormatters.forPattern(format).format(javaDate); String jodaTimeOut = Joda.forPattern(format).printer().print(jodaDate); String message = String.format(Locale.ROOT, "expected string representation to be equal for format [%s]: joda [%s], java [%s]", format, jodaTimeOut, javaTimeOut); diff --git a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java index 1dca4b8e4b922..f01db140a7057 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java @@ -31,7 +31,6 @@ public class DateFormattersTests extends ESTestCase { - // the epoch milli parser is a bit special, as it does not use date formatter, see comments in DateFormatters public void testEpochMilliParser() { DateFormatter formatter = DateFormatters.forPattern("epoch_millis"); @@ -74,6 +73,6 @@ private void assertThatSameDateTime(DateFormatter formatter, DateFormatter zoned private void assertSameFormat(DateFormatter formatter, long millis) { String millisAsString = String.valueOf(millis); TemporalAccessor accessor = formatter.parse(millisAsString); - assertThat(millisAsString, is(formatter.print(accessor))); + assertThat(millisAsString, is(formatter.format(accessor))); } } diff --git a/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java b/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java index 176704442c73d..66e68b0aad049 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateMathParserTests.java @@ -300,7 +300,7 @@ private void assertDateEquals(long gotMillis, String original, String expected) ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(gotMillis), ZoneOffset.UTC); fail("Date math not equal\n" + "Original : " + original + "\n" + - "Parsed : " + formatter.print(zonedDateTime) + "\n" + + "Parsed : " + formatter.format(zonedDateTime) + "\n" + "Expected : " + expected + "\n" + "Expected milliseconds : " + expectedMillis + "\n" + "Actual milliseconds : " + gotMillis + "\n"); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java index 05af24baf6d45..eeb6e12161383 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/MinDocCountIT.java @@ -124,7 +124,7 @@ public void setupSuiteScopeCluster() throws Exception { double doubleTerm = longTerm * Math.PI; ZonedDateTime time = ZonedDateTime.of(2014, 1, ((int) longTerm % 20) + 1, 0, 0, 0, 0, ZoneOffset.UTC); - String dateTerm = DateFormatters.forPattern("yyyy-MM-dd").print(time); + String dateTerm = DateFormatters.forPattern("yyyy-MM-dd").format(time); final int frequency = randomBoolean() ? 1 : randomIntBetween(2, 20); for (int j = 0; j < frequency; ++j) { indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder() diff --git a/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java b/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java index 0d088d5b1a9af..45b6340ba6f46 100644 --- a/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java +++ b/server/src/test/java/org/elasticsearch/search/fields/SearchFieldsIT.java @@ -553,7 +553,7 @@ public void testStoredFieldsWithoutSource() throws Exception { .field("long_field", 4L) .field("float_field", 5.0f) .field("double_field", 6.0d) - .field("date_field", DateFormatters.forPattern("dateOptionalTime").print(date)) + .field("date_field", DateFormatters.forPattern("dateOptionalTime").format(date)) .field("boolean_field", true) .field("binary_field", Base64.getEncoder().encodeToString("testing text".getBytes("UTF-8"))) .endObject()).execute().actionGet(); @@ -585,7 +585,7 @@ public void testStoredFieldsWithoutSource() throws Exception { assertThat(searchHit.getFields().get("long_field").getValue(), equalTo((Object) 4L)); assertThat(searchHit.getFields().get("float_field").getValue(), equalTo((Object) 5.0f)); assertThat(searchHit.getFields().get("double_field").getValue(), equalTo((Object) 6.0d)); - String dateTime = DateFormatters.forPattern("dateOptionalTime").print(date); + String dateTime = DateFormatters.forPattern("dateOptionalTime").format(date); assertThat(searchHit.getFields().get("date_field").getValue(), equalTo((Object) dateTime)); assertThat(searchHit.getFields().get("boolean_field").getValue(), equalTo((Object) Boolean.TRUE)); assertThat(searchHit.getFields().get("binary_field").getValue(), equalTo(new BytesArray("testing text" .getBytes("UTF8")))); @@ -765,7 +765,7 @@ public void testDocValueFields() throws Exception { .field("long_field", 4L) .field("float_field", 5.0f) .field("double_field", 6.0d) - .field("date_field", DateFormatters.forPattern("dateOptionalTime").print(date)) + .field("date_field", DateFormatters.forPattern("dateOptionalTime").format(date)) .field("boolean_field", true) .field("binary_field", new byte[] {42, 100}) .field("ip_field", "::1") @@ -865,7 +865,7 @@ public void testDocValueFields() throws Exception { assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo((Object) 5.0)); assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo((Object) 6.0d)); assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(), - equalTo(DateFormatters.forPattern("dateOptionalTime").print(date))); + equalTo(DateFormatters.forPattern("dateOptionalTime").format(date))); assertThat(searchResponse.getHits().getAt(0).getFields().get("boolean_field").getValue(), equalTo((Object) true)); assertThat(searchResponse.getHits().getAt(0).getFields().get("text_field").getValue(), equalTo("foo")); assertThat(searchResponse.getHits().getAt(0).getFields().get("keyword_field").getValue(), equalTo("foo")); @@ -895,7 +895,7 @@ public void testDocValueFields() throws Exception { assertThat(searchResponse.getHits().getAt(0).getFields().get("float_field").getValue(), equalTo("5.0")); assertThat(searchResponse.getHits().getAt(0).getFields().get("double_field").getValue(), equalTo("6.0")); assertThat(searchResponse.getHits().getAt(0).getFields().get("date_field").getValue(), - equalTo(DateFormatters.forPattern("epoch_millis").print(date))); + equalTo(DateFormatters.forPattern("epoch_millis").format(date))); } public void testScriptFields() throws Exception {