Skip to content

Commit 1c32497

Browse files
authored
Date: Add DateFormatters class that uses java.time (#31856)
A newly added class called DateFormatters now contains java.time based builders for dates, which also intends to be fully backwards compatible, when the name based date formatters are picked. Also a new class named CompoundDateTimeFormatter for being able to parse multiple different formats has been added. A duelling test class has been added that ensures the same dates when parsing java or joda time formatted dates for the name based dates. Note, that java.time and joda time are not fully backwards compatible, which also means that old formats will currently not work with this setup.
1 parent 991d17c commit 1c32497

File tree

6 files changed

+1529
-10
lines changed

6 files changed

+1529
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.common.time;
20+
21+
import java.time.ZoneId;
22+
import java.time.format.DateTimeFormatter;
23+
import java.time.format.DateTimeParseException;
24+
import java.time.temporal.TemporalAccessor;
25+
26+
/**
27+
* wrapper class around java.time.DateTimeFormatter that supports multiple formats for easier parsing,
28+
* and one specific format for printing
29+
*/
30+
public class CompoundDateTimeFormatter {
31+
32+
final DateTimeFormatter printer;
33+
final DateTimeFormatter[] parsers;
34+
35+
CompoundDateTimeFormatter(DateTimeFormatter ... parsers) {
36+
if (parsers.length == 0) {
37+
throw new IllegalArgumentException("at least one date time formatter is required");
38+
}
39+
this.printer = parsers[0];
40+
this.parsers = parsers;
41+
}
42+
43+
public TemporalAccessor parse(String input) {
44+
DateTimeParseException failure = null;
45+
for (int i = 0; i < parsers.length; i++) {
46+
try {
47+
return parsers[i].parse(input);
48+
} catch (DateTimeParseException e) {
49+
if (failure == null) {
50+
failure = e;
51+
} else {
52+
failure.addSuppressed(e);
53+
}
54+
}
55+
}
56+
57+
// ensure that all parsers exceptions are returned instead of only the last one
58+
throw failure;
59+
}
60+
61+
public CompoundDateTimeFormatter withZone(ZoneId zoneId) {
62+
final DateTimeFormatter[] parsersWithZone = new DateTimeFormatter[parsers.length];
63+
for (int i = 0; i < parsers.length; i++) {
64+
parsersWithZone[i] = parsers[i].withZone(zoneId);
65+
}
66+
67+
return new CompoundDateTimeFormatter(parsersWithZone);
68+
}
69+
70+
public String format(TemporalAccessor accessor) {
71+
return printer.format(accessor);
72+
}
73+
}

0 commit comments

Comments
 (0)