I'm trying to use the DateTimeFormatter API to show a specific time in a TextView but whenever I run my app whilst my device is set to either the Arabic or Bengali language, the time always seems to show western numerals for some reason. Is this intentional, or is there something that I need to do to fix this?
Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val localizedTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
val timeOpen = LocalTime.of(9, 30)
val timeClose = LocalTime.of(17, 15)
tv_time.text = (timeOpen.format(localizedTimeFormatter) +
"\n\n" + timeClose.format(localizedTimeFormatter))
}
}
Clock app (Arabic)

My app (Arabic)

Clock app (Bengali)

My app (Bengali)

This is as designed. Instances of DateTimeFormatter, even the localized ones, use Western/ASCII digits unless explicitly instructed otherwise. The instruction is not hard when you know how:
DateTimeFormatter localizedTimeFormatter = DateTimeFormatter
.ofLocalizedTime(FormatStyle.SHORT)
.withDecimalStyle(DecimalStyle.ofDefaultLocale());
LocalTime timeOpen = LocalTime.of(9, 30);
LocalTime timeClose = LocalTime.of(17, 15);
String text = timeOpen.format(localizedTimeFormatter)
+ '\n' + timeClose.format(localizedTimeFormatter);
System.out.println(text);
Output in Bengali locale (bn-BD):
৯:৩০ AM ৫:১৫ PM
And in Arabic (ar):
٩:٣٠ ص ٥:١٥ م
I hope you can translate from my Java code yourself. I believe that it shouldn’t be hard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With