2

I have a date in String format, which I parse using SimpleDateFormat, but to my surprise I keep getting java.text.ParseException: Unparseable date: Error.

I thought I was getting the pattern wrong, but I looked closely & i don't think so, I'm wondering what my issue might be:

I Keep getting

E/FormatFormDate: java.text.ParseException: Unparseable date: "2019-02-25T22:43:23.213Z"

This is my code below:

var clean = "2019-02-25T22:43:23.213Z"

val inputFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
val outputFormatTime = "HH:mm"
val DATE_TIME_ONLY = SimpleDateFormat(outputFormatTime, Locale.getDefault())
if (clean != "") {
    try {
        val parseDate = SimpleDateFormat(inputFormat, Locale.getDefault()).parse(clean)
        clean = DATE_TIME_ONLY.format(parseDate)
        Log.d("TAG", clean)

    } catch (e: ParseException) {
        Log.e("FormatFormDate", Log.getStackTraceString(e))
    }
}
3
  • Possible duplicate of Parse Date String in Java Commented Feb 28, 2019 at 19:45
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome SimpleDateFormat and friends, and adding ThreeTenABP to your Android project in order to use java.time, the modern Java date and time API. It is so much nicer to work with. Commented Feb 28, 2019 at 19:45
  • Yes @OleV.V. I hardly ever spotted my problem because I believed my pattern was correct, so even though I saw that answer. I thought it wasn't for me. Thanks for the Jake Wharton link, Let me check it out & try to update my answer if possible Commented Feb 28, 2019 at 22:05

1 Answer 1

2

I have found my Issue:

My pattern was wrong, I was meant to do this:

val inputFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

But Instead I was doing this:

val inputFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

Notice the single quotes with 'Z'

Sign up to request clarification or add additional context in comments.

1 Comment

Wrong answer. Z is a UTC offset of zero and must be parsed as such, or you get the wrong time (on the vast majority of Java installations).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.