如何從日期轉換yyyy/MM/dd到dd/MM/YYY下面的函式給出錯誤
fun convertTime(date: String): String {
val sdf = SimpleDateFormat("dd/MM/yyyy", Locale("pt", "BR"))
sdf.isLenient = false
val d: Date = sdf.parse(date)!!
return sdf.format(d)
}
錯誤:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quitanda, PID: 10526
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.text.ParseException: Unparseable date: "2021-11-18T23:39:45.000Z"
at java.text.DateFormat.parse(DateFormat.java:362)
uj5u.com熱心網友回復:
將您的輸入決議為 a ZonedDateTime,然后根據需要應用 a 對其DateTimeFormatter進行格式化:
fun convertTime(date: String): String {
val zonedDateTime = ZonedDateTime.parse(date)
val dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale("pt", "BR"))
return dtf.format(zonedDateTime)
}
避免使用 JavaDate并考慮使用新的 Java 日期 API ( https://www.baeldung.com/java-8-date-time-intro )。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361489.html
上一篇:是否可以將屬性值傳遞給注釋?
