代码之家  ›  专栏  ›  技术社区  ›  Sazzad Hissain Khan

Kotlin-SimpleDataFormat解析需要无限的时间

  •  0
  • Sazzad Hissain Khan  · 技术社区  · 5 年前

    我正在尝试用解析日期字符串 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 SDK:28

    2 回复  |  直到 5 年前
        1
  •  3
  •   Pavya    5 年前

    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
            }
        }
    
        2
  •  2
  •   Vivek Mishra    5 年前

    您的日期格式不正确。应如下所示

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    

    '