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

Rodrigo Uchôa rodrigo.uchoa at gmail.com
Tue Mar 3 01:55:22 UTC 2015


Roger and Leif, thank you. Both answers are really helpful.

Leif, as a matter of fact, I'm also trying to develop a localized parser
for a Date Picker control. But in my case it's for Apache Wicket. The
existing one is based on the old "java.util" APIs.

Best regards.

On Mon, Mar 2, 2015 at 8:09 PM, Leif Samuelsson <leif.samuelsson at oracle.com>
wrote:

>
> 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