The java.time.format.DateTimeFormatter
class does not work correctly for locales that use different symbols to represent numbers.
Languages such as Persian (also called Farsi) use different symbols and Unicode characters to represent numbers:
Persian numerals: ۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹
Western numerals: 0 1 2 3 4 5 6 7 8 9
For example:
Locale myLocale = Locale.forLanguageTag("fa");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy", myLocale);
formatter.format(LocalDate.now()); // should return "۲۰۲۰" but returns "2020"
I should say that the DateFormat
class that is used to format Java legacy Date
s works as expected.
Related: What is the difference between withLocale() and localizedBy() in Java DateTimeFormatter?
This is fixed in JDK 15. Use localizedBy()
(and NOT localizedWith()
):
formatter
.localizedBy(myLocale)
.format(...)
For Java versions < 15 format like this:
formatter
.localizedBy(myLcoale)
.withDecimalStyle(DecimalStyle.of(myLocale))
.format(...)