Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original pattern String given a JDK 8 DateTimeFormatter?

Related to my question here - how do I get the original pattern String given a DateTimeFormatter?

like image 239
pathikrit Avatar asked Mar 09 '15 18:03

pathikrit


3 Answers

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.

like image 136
assylias Avatar answered Nov 15 '22 21:11

assylias


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.

like image 41
Jaap Geurts Avatar answered Nov 15 '22 21:11

Jaap Geurts


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:

java.time.format.DateTimeFormatter variousFormatPatterns =
    java.time.format.DateTimeFormatter.ofPattern("yy MM dd");
System.out.println("Test 1: " + variousFormatPatterns.toString() );

variousFormatPatterns = java.time.format.DateTimeFormatter.ofPattern("yy-MM-dd");
System.out.println("\nTest 2: " + variousFormatPatterns.toString() );

variousFormatPatterns = java.time.format.DateTimeFormatter.ofPattern("yyMMdd");
System.out.println("\nTest 3: " + variousFormatPatterns.toString() );

variousFormatPatterns = java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
System.out.println("\nTest 4: " + variousFormatPatterns.toString() );

Results (note the retention of the space/hyphen/slash/colon delimiter characters):

Test 1: ReducedValue(YearOfEra,2,2,2000-01-01)' 'Value(MonthOfYear,2)' 'Value(DayOfMonth,2)

Test 2: ReducedValue(YearOfEra,2,2,2000-01-01)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)

Test 3: ReducedValue(YearOfEra,2,2,2000-01-01)Value(MonthOfYear,2)Value(DayOfMonth,2)

Test 4: Value(MonthOfYear,2)'/'Value(DayOfMonth,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD)' 'Value(HourOfDay,2)':'Value(MinuteOfHour,2)':'Value(SecondOfMinute,2)

Implementing this approach would require studying the code in java.time.format.DateTimeFormatterBuilder. The JavaDoc for the appendPattern(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 as Value, ReducedValue, Fraction, etc. could change without notice in future Java versions.

like image 2
GoldDragonTSU Avatar answered Nov 15 '22 23:11

GoldDragonTSU