Formatting a LocalDate in Java 8 using a specific Locale can be done as follows:
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 will result in:
// 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 separator ("-", ".", "/", "", Etc.) to use and how to sort date elements (for example, month to day or vice versa, etc. in some places it may be that year in the first place, etc.).
My question is: How can I format java.time.YearMonth and java.time.MonthDay depending on the Locale , as in the example above?
Based on this 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 may be other Locale that use completely different delimiters and orderings.
Thanks!