嗨,我想實作像“2021 ??????”這樣的日期格式,有人可以幫我實作這種格式嗎?
這是我的示例源代碼
SimpleDateFormat dateFormatter = new SimpleDateFormat("d MMM yyyy", locale);
dateString = dateFormatter.format(date);
uj5u.com熱心網友回復:
將 Locale 設定為“ar”會將日期格式從英語轉換為阿拉伯語。
我也用過DateTimeFormatter,因為SimpleDateFormat已經過時了
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 以下使用它啟用脫糖
輸出: 2021, ?????? 3
uj5u.com熱心網友回復:
日期語言由系統語言決定,但要以特定語言顯示日期,不依賴于系統語言。你可以這樣做
TextView arabicText = findViewById(R.id.arabic);
TextView englishText = findViewById(R.id.english);
Date date = new Date();
SimpleDateFormat arDate = new SimpleDateFormat("d MMM yyyy", new Locale("ar"));
SimpleDateFormat enDate = new SimpleDateFormat("d MMM yyyy", new Locale("en"));
String ar = arDate.format(date);
String en = enDate.format(date);
arabicText.setText(ar);
englishText.setText(en);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/363555.html
上一篇:在TimelineHighcharts中顯示TIME而不是DATE
下一篇:回傳價格最高的第一個日期時間
