[threeten-dev] Clarification request: DateTimeFormatter.ofLocalizedDate/Time behavior

Stephen Colebourne scolebourne at joda.org
Mon Feb 4 02:49:46 PST 2013


On 4 February 2013 06:05, Masayoshi Okutsu <masayoshi.okutsu at oracle.com> wrote:
> What's the expected behavior of the following code?
>
>   DateTimeFormatter dtf =
> DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
>   System.out.println(dtf.format(JapaneseChronology.INSTANCE.dateNow()));
>
> Currently, the format pattern is taken from a DateFormat instance if it's
> actually a SimpleDateFormat. The DateFormat.get*Instance behavior is
> affected by the specified Locale for selecting the calendar system to use.
> For example, if the default Locale is "ja-JP-u-ca-japanese", the format
> pattern from a SimpleDateFormat is for the Japanese calendar system. But
> that doesn't seem to be the intended behavior (relying on the locale's
> calendar).

The current implementation is in DateTimeFormatStyleProvider. It has a
"TODO" to make it better.

The code currently uses the locale of the formatter to perform the
lookup, and the chronology (which is available) is unused.

Ideally, DateTimeFormatStyleProvider should be reworked entirely to
access the CLDR data more directly.

So, given this code:
str = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(loc).format(chronoDate);

I would expect that both the chronology of the date and the specified
locale would influence the pattern that is picked, and thus the text
output. The chronology should be used to pick the calendar system of
the pattern, and the locale should be used to pick the English vs
French vs Japanese view of the pattern (or even a different pattern).

My feeling is that this could be implemented by changing the locale in
DateTimeFormatStyleProvider to add the "ca" variation from the
chronology object.

JapaneseDate + locale jp_JP => locale jp_JP-u-ca-japanese

However, you raise an interesting question of what if the locale
includes a "ca" block already. The simple case is:
 JapaneseDate + locale jp_JP-japanese => locale jp_JP-u-ca-japanese

the complex cases are:
 JapaneseDate + locale jp_JP-minguo => ?
 MinguoDate + locale jp_JP-japanese => ?

I think you have more knowledge than me to decide on behaviour there
(taking into account what data is available in CLDR)

Stephen


More information about the threeten-dev mailing list