如何以英文和阿拉伯文显示日期格式? [英] How to show date format in both english and arabic?

查看:0
本文介绍了如何以英文和阿拉伯文显示日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想实现这样的日期格式:2021年نوفمبر"; 有谁能帮助我实现这种格式吗?

以下是我的示例源代码

 SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMM yyyy", locale);
            dateString = dateFormatter.format(date);

推荐答案

将区域设置设置为&ar&q;会将日期格式从英语转换为阿拉伯语。
我还使用了DateTimeFormatter,因为SimpleDateFormatoutdated

fun parseDate() {
    var formatter: DateTimeFormatter? = null
    val date = "2021-11-03T14:09:31"
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
        val dateTime: LocalDateTime = LocalDateTime.parse(date, formatter)
        val formatter2: DateTimeFormatter =
            DateTimeFormatter.ofPattern(
                "yyyy, MMM d", Locale("ar")  // set the language in which you want to convert
                                            // For english use Locale.ENGLISH
            )
        Log.e("Date", "" + dateTime.format(formatter2))
    }
}

注意:DateTimeFormatter仅适用于Android 8及以上版本,如需在Android 8以下启用desugaring

输出:2021,نوفمبر3

编辑::如果要将数字也转换为阿拉伯语,请将withDecimalStyleDateTimeFormatter一起使用。

 val formatter2: DateTimeFormatter =
                DateTimeFormatter.ofPattern(
                    "d MMM, yyyy",
                    Locale("ar")
                ).withDecimalStyle(
                    DecimalStyle.of(Locale("ar"))
                )

输出:٣نوفمبر,٢٠٢١

这篇关于如何以英文和阿拉伯文显示日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆