3

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

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
  • java.text.ParseException: Unparseable date: "2006-04-16T04:00:00Z" Its giving error – Pavya Jun 20 '19 at 04:11
  • Its giving parse Exception directly .. Use `yyyy-MM-dd'T'HH:mm:ss'Z'` cause your date does not have value for ZONE .. Use `DateTime` API of Java 8 or some backport like `ThreeTenBP` for date operations.. – ADM Jun 20 '19 at 04:16
  • There's a `'` missing before and after `Z`. Shouldn't it be like this: `yyyy-MM-dd'T'HH:mm:ss'Z'` in this code? – SaadAAkash Jun 20 '19 at 04:18
  • Possible sort of duplicate of [ISO 8601 String to Date/Time object in Android](https://stackoverflow.com/questions/3941357/iso-8601-string-to-date-time-object-in-android) – Ole V.V. Jun 20 '19 at 08:29

4 Answers4

5

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
        }
    }
Pavya
  • 6,015
  • 4
  • 29
  • 42
  • Wrong. The `Z` is an offset (of 0) form UTC and needs to be parsed as such (not as a literal), or you will get wrong times on the vast majority of JVMs. – Ole V.V. Jun 24 '19 at 11:31
2

Your date format is incorrect. It should be as shown below

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

Notice the enclosing ' with Z. You are missing that in your date format.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0

I had a similar issue and my problem was that I was importing the wrong ParseException. You need to make sure you are importing the java.text parse exception.

Check the import code at the top of your file.

CORRECT ParseException

import java.text.ParseException

INCORRECT ParseException

import android.net.ParseException //Android example

Your date formats weren't lining up either, so of course that also needed to be fixed. But ideally you want the try/catch block to stop your program from crashing if the date strings are ever bad, so it's a good idea to test it with bad data.

iOS_Mouse
  • 754
  • 7
  • 13
0

The timezone here actually is using ISO 8601 format. To correctly parse this by Java SimpleDateFormat you should use the below pattern: "yyyy-MM-dd'T'HH:mm:ssX"

Here X means time zone in ISO 8601 format. See https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Tim Chan
  • 1
  • 1