Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeFormatter not using native numerals in Arabic nor Bengali

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)

enter image description here

My app (Arabic)

enter image description here

Clock app (Bengali)

enter image description here

My app (Bengali)

enter image description here

like image 528
wbk727 Avatar asked Oct 23 '25 09:10

wbk727


1 Answers

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.

like image 114
Ole V.V. Avatar answered Oct 25 '25 22:10

Ole V.V.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!