-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date: Add DateFormatters class that uses java.time #31856
Changes from 2 commits
d28d8b8
715137d
5007a93
cd1e2df
fc24da8
e37d99a
d37c2ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* wrapper class around java.time.DateTimeFormatter that supports multiple formats for easier parsing | ||
*/ | ||
public class DateFormatter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be named more transparently, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renamed |
||
|
||
private DateTimeFormatter printer; | ||
private DateTimeFormatter[] parsers; | ||
|
||
public DateFormatter(DateTimeFormatter printer, DateTimeFormatter ... parsers) { | ||
this.printer = printer; | ||
this.parsers = parsers; | ||
} | ||
|
||
public TemporalAccessor parse(String input) { | ||
if (parsers.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check this in the ctor? If no parsers are passed, then set parsers to a single sized array of printer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
for (int i = 0; i < parsers.length; i++) { | ||
try { | ||
return parsers[i].parse(input); | ||
} catch (DateTimeParseException e) {} | ||
} | ||
} | ||
|
||
return printer.parse(input); | ||
} | ||
|
||
public DateFormatter withZone(ZoneId zoneId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be implemented in the same way, but by calling the constructor with the resulting formatters? Then the formatter members can be final. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatters are now final |
||
printer = printer.withZone(zoneId); | ||
for (int i = 0; i < parsers.length; i++) { | ||
parsers[i] = parsers[i].withZone(zoneId); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public String format(TemporalAccessor accessor) { | ||
return printer.format(accessor); | ||
} | ||
|
||
// for internal use only | ||
private DateTimeFormatter[] all() { | ||
return Stream.concat(Arrays.stream(new DateTimeFormatter[] { printer }), Arrays.stream(parsers)).toArray(DateTimeFormatter[]::new); | ||
} | ||
|
||
public static DateFormatter merge(DateFormatter ... dateFormatters) { | ||
DateTimeFormatter[] parsers = Arrays.stream(dateFormatters).map(DateFormatter::all) | ||
.flatMap(Arrays::stream).toArray(DateTimeFormatter[]::new); | ||
return new DateFormatter(dateFormatters[0].printer, parsers); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be more clear the multiple formats has to do with parsing, not printing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the comment