我试图用SimpleDateFormat
解析日期字符串,它从不停止,也不会出现任何异常。请看下面的代码,
fun getDate(dateStr: String) {
try {
/** DEBUG dateStr = '2006-04-16T04:00:00Z' **/
val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH)
val mDate = formatter.parse(dateStr) // this never ends while debugging
} catch (e: Exception){
Logger.e("Error $e") // this never gets called either
}
}
可能的问题是什么?
注:我正在使用,
Android Studio: 3.4.1,Kotlin版本: 1.3.31,Min SDK: 23,Target SDK: 28,Compile : 28
发布于 2019-06-19 20:15:44
使用以下功能
fun getDate(dateStr: String) {
try {
/** DEBUG dateStr = '2006-04-16T04:00:00Z' **/
val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH)
val mDate = formatter.parse(dateStr) // this never ends while debugging
Log.e("mDate", mDate.toString())
} catch (e: Exception){
Log.e("mDate",e.toString()) // this never gets called either
}
}
发布于 2019-06-19 20:26:39
您的日期格式不正确。它应该如下所示
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
请注意以Z作为附件的'
。您缺少了日期格式的内容。
发布于 2021-03-17 11:53:00
我也有类似的问题,我的问题是我导入了错误的ParseException。您需要确保正在导入java.text解析异常。
检查文件顶部的导入代码。
更正ParseException
import java.text.ParseException
不正确的ParseException
import android.net.ParseException //Android example
您的日期格式也没有排队,所以当然也需要修正。但是,理想情况下,您希望try/catch块能够阻止程序在日期字符串坏的情况下崩溃,所以最好用坏数据来测试它。
https://stackoverflow.com/questions/56678746
复制相似问题