Formatting a LocalDate
in Java 8 using a specific Locale
can be achieved like this:
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(myLocale).format(value);
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(myLocale).format(value);
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(myLocale).format(value);
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(myLocale).format(value);
Assuming value = LocalDate.now()
this would result into:
// myLocale = Locale.ENGLISH
6/30/16
Jun 30, 2016
June 30, 2016
Thursday, June 30, 2016
// myLocale = Locale.GERMAN
30.06.16
30.06.2016
30. Juni 2016
Donnerstag, 30. Juni 2016
// myLocale = new Locale("es", "ES")
30/06/16
30-jun-2016
30 de junio de 2016
jueves 30 de junio de 2016
As you can see Java decides which delimiter ("-", ".", "/", " ", etc.) to use and how to sort the date elements (e.g. month before day or vice versa, etc - in some locales it could be that year comes first, etc.).
My question is: How can I format a java.time.YearMonth
and java.time.MonthDay
depending on a Locale
like the example above?
Based on the example I would expect results like this...
... for YearMonth
:
// myLocale = Locale.ENGLISH
6/16
Jun, 2016
June, 2016
June, 2016
// myLocale = Locale.GERMAN
06.16
06.2016
Juni 2016
Juni 2016
// myLocale = new Locale("es", "ES")
06/16
jun-2016
de junio de 2016
de junio de 2016
... for MonthDay
:
// myLocale = Locale.ENGLISH
6/30
Jun 30
June 30
June 30
// myLocale = Locale.GERMAN
30.06.
30.06.
30. Juni
30. Juni
// myLocale = new Locale("es", "ES")
30/06
30-jun
30 de junio de
30 de junio de
Of course there could be other Locale
s which use completely different delimiters and ordering.
Thank you!
It seems that this Java-8-bug cannot be fixed in Java-9 because even the feature-extension-complete-date is already over. Let's see if it is going to be fixed in Java-10 which is still a long time away...
Of course, as one answer here suggests, you could try to process a given localized date pattern in order to remove irrelevant parts. But I still consider this approach as errorprone because there are still so many locales around. And indeed, the accepted answer is flawed for Chinese. Localized literals are here the main problem. Maybe the accepted answer can be fixed at least for this important language, but you might also consider two other libraries with good internationalization features which can solve your issue in a more reliable way.
a) ICU4J
However, one problem is lack of interoperability with Java-8-types, especially
MonthDay
andYearMonth
. A solution requires something likeDate.from(YearMonth.now().atDay(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
Possible, but cumbersome.b) my library Time4J (with the same data base as ICU4J)
Interoperability with Java-8 exists in the reverse direction, too. And the Time4J-counterpart for
MonthDay
is the classAnnualDate
.Side note: The accepted answer of @Julian yields for Chinese: 2017年1 (needs to be fixed)
You need to use
DateTimeFormatter#ofPattern
For
YearMonth
For
MonthDay
Solution provided by gevorg is probably the simplest solution if you will use a limited list of Locale.
If you want to make it work with any Locale I would suggest to get a locale pattern and then remove the parts you are not interested in, once you have this pattern you should remove the part you are not interested in and use the resulting pattern to create your own DateTimeFormatter.
This is a full example of the idea explained above for
MonthDay
. In order to use it forYearMonth
replacekeep.add('d')
withkeep.add('y')
. (and of courseMonthDay
withYearMonth
)The output would be:
And for YearMonth: