Related to my question here - how do I get the original pattern
String given a DateTimeFormatter
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Not a straightforward or elegant solution, but using the results of the
DateTimeFormatter
's.toString()
method, it might be possible to roll your own code that parses out the resulting String and reconstructs the original pattern.Some code that prints a few
.toString()
results for various patterns:Results (note the retention of the space/hyphen/slash/colon delimiter characters):
Implementing this approach would require studying the code in
java.time.format.DateTimeFormatterBuilder
. The JavaDoc for theappendPattern(String pattern)
method appears to be particularly useful. You might be able to take some shortcuts if you know you are working with only a few types of patterns.Taking a quick glance through the
DateTimeFormatterBuilder
code, there would likely be a risk relying on this type of solution as the Strings such asValue
,ReducedValue
,Fraction
, etc. could change without notice in future Java versions.It may not be a direct answer to your question but it may help.
If you know the parameters of how it the formatter was constructed you can call the static method:
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle, Chronology chrono, Locale locale)
This will give you the pattern as a string.
It's been asked on the mailing list and the answer is that it is not possible because the original pattern is not retained.
The same thread suggests using a
DateTimeFormatterBuilder
which does have the information.