[threeten-dev] How to get pattern as a String from DateTimeFormatter

Leif Samuelsson leif.samuelsson at oracle.com
Mon Mar 2 23:09:55 UTC 2015


I needed this when developing a lenient, localized parser for the JavaFX DatePicker
control. I arrived at the following code, based on the JDK 8 API:

     String pattern =
         DateTimeFormatterBuilder.getLocalizedDateTimePattern(dateStyle, timeStyle,
                                                              chronology, locale);
     return new DateTimeFormatterBuilder().parseLenient()
                                          .appendPattern(pattern)
                                          .toFormatter()
                                          .withChronology(chronology)
                                          .withDecimalStyle(DecimalStyle.of(locale));

This is now encapsulated in javafx.util.converter.LocalDateTimeStringConverter
and LocalDateStringConverter, as of JDK 8u40.

Leif

On 2015-03-02 15:05, Roger Riggs wrote:
 > Hi,
 >
 > Take a look at java.time.DateTimeFormatterBuilder.getLocalizedDateTimePattern.
 > It may provide the components you need.
 >
 > http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html#getLocalizedDateTimePattern-java.time.format.FormatStyle-java.time.format.FormatStyle-java.time.chrono.Chronology-java.util.Locale-
 >
 > Roger
 >
 > On 3/2/2015 5:13 PM, Rodrigo Uchôa wrote:
 >> Interesting.
 >>
 >> My problem is, I need to the "FormatStyle.SHORT" pattern as a String from a
 >> specified locale at runtime. Is there any other way?
 >>
 >> On Mon, Mar 2, 2015 at 4:38 PM, Stephen Colebourne <scolebourne at joda.org>
 >> wrote:
 >>
 >>> The pattern is not retained, so this cannot be done.
 >>> Stephen
 >>>
 >>> On 2 March 2015 at 18:50, Rodrigo Uchôa <rodrigo.uchoa at gmail.com> wrote:
 >>>> After building an instance of DateTimeFormatter, is it possible to
 >>> extract
 >>>> from it the pattern as a String?
 >>>>
 >>>> Something like this:
 >>>>
 >>>> DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
 >>>> String pattern = formatter.toPattern(); //example
 >>>>
 >>>> Is there any way this can done?
 >>>>
 >>>> Regards!
 >



More information about the threeten-dev mailing list