DateTimeFormatter不在阿拉伯语或孟加拉语中使用本机数字

问题描述

我正在尝试使用DateTimeFormatter API在TextView显示特定时间,但是每当我将设备设置为阿拉伯语或孟加拉语时运行我的应用程序,时间总是似乎出于某种原因显示了西方数字。这是故意的,还是我需要解决此问题?

科特林

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))
    }
}

时钟应用程序(阿拉伯语)

enter image description here

我的应用程序(阿拉伯语)

enter image description here

时钟应用程序(孟加拉)

enter image description here

我的应用程序(孟加拉)

enter image description here

解决方法

这是按设计的。除非另有明确指示,否则DateTimeFormatter的实例(甚至是本地化的实例)也使用Western / ASCII数字。当您知道如何时,说明并不难:

    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);

孟加拉语区域(bn-BD)中的输出:

৯:৩০ AM
৫:১৫ PM

以及阿拉伯语(ar):

٩:٣٠ ص
٥:١٥ م

希望您可以自己翻译我的Java代码。我相信这应该不难。