Search code examples
androiddatetimedebuggingkotlinsimpledateformat

Kotlin - SimpleDateFormat parsing takes infinite time


I am trying to parse date string with SimpleDateFormat which never stops nor gives any exception. Please see the code below,

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
    }
}

What can be the possible issue?

Note: I am using,

Android Studio: 3.4.1, Kotlin version: 1.3.31, Min SDK: 23, Target SDK: 28, Compile SDK: 28


Solution

  • Use below function

    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
            }
        }