下午好,我想把我的simpledateformat從 "15/09/21 "轉換為 "4月1日,星期二",我想知道如何把它轉換為這個樣子?我附上了一張圖片,是之前的樣子。
我呼叫日期的代碼:
addAll(pending.sortedByDescending { it.finishDate.convertToDate()? .time })
addAll(accepted.sortedByDescending { it.finishDate.convertToDate()?.time })
時間被呼叫的地方:
fun String.convertToDate(): 日期?{
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"/span>)
return try {
sdf.parse(this)
} catch (e: ParseException) {
Timber.e("錯誤的日期格式 - 決議不可能")
null。
}
}
uj5u.com熱心網友回復:
試試這個
fun changedate( olddate: String):String{
var changedDate:String = "": String{
try {
val dateFormatprev = SimpleDateFormat("dd/MM/yyyy"/span>)
val d.Date = dateFormatprev.parse(olddate)
val dateFormat = SimpleDateFormat("EEe, MMM dd")
changedDate = dateFormat.format(d)
println(changedDate)
} catch (e: ParseException) {
e.printStackTrace()
}
return changedDate
}
使用情況
Log.e("TAG",changeate("01/01/2021")
uj5u.com熱心網友回復:
SimpleDateFormat早就過時了,而且很麻煩,正如@Ole V所建議的那樣,更新為DateTimeFormatter ,這里有一個相同的示例代碼
DateTimeFormatter formatter = null;
String date = "2021-09-16T11:36:15"。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"/span>)。
LocalDateTime dateTime = LocalDateTime.parse(date, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("EEE, MMM d"); //使用MMMM顯示全月名稱。
Log.e("Date", "" dateTime.format(formatter2) ) 。
}
要在android 8下面使用這個,使用desugaring
輸出9月16日,星期四
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/333705.html
標籤:
