javaandroidkotlindategmt

how convert GMT date String into Date


I have a GMT String "Thr, 09 Jun 2022 19:20:00 GMT"

I want to convert this into Date.

Can anyone help how I convert this?

I tried every way but giving Unparseable date: "Thr, 09 Jun 2022 19:20:00 GMT"

Here is the code:

private fun getDate(time: String): Date? {
    val pattern = "EEE, dd MMM yyyy HH:mm:ss Z"
    val format = SimpleDateFormat(pattern)
    var javaDate: Date? = null
    try {
        javaDate = format.parse(time)
    } catch (e: ParseException) {
        e.printStackTrace()
    }
    return javaDate

}

I referred to this question.


Solution

  • The site answer you're referring to just works.

    import java.text.*
    
    fun main() {
        val rfcDate = "Sat, 13 Mar 2010 11:29:05 -0800";
        val pattern = "EEE, dd MMM yyyy HH:mm:ss Z";
        val format = SimpleDateFormat(pattern);
        val javaDate = format.parse(rfcDate);
        println(javaDate)
    }
    

    You can run the example online over here

    So it's likely that your input string doesn't match such pattern.

    And indeed Thr is not valid. It should be Thu. Here's the updated playground