0

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
android dev
  • 200
  • 5
  • 18
  • 1
    Show us your non-working code – Some random IT boy Jun 09 '22 at 12:48
  • Show us the input and the exception thrown – Some random IT boy Jun 09 '22 at 12:52
  • "Thr, 09 Jun 2022 19:20:00 GMT"-->input and exception is java.text.ParseException: Unparseable date: "Thr, 09 Jun 2022 19:20:00 GMT" @SomerandomITboy – android dev Jun 09 '22 at 12:53
  • You will not be able to parse this because the expected abbreviation for an English *Thursday* is *Thu* but your `String` starts with `Thr`. – deHaar Jun 09 '22 at 12:57
  • 2
    Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. Use [desugaring](https://developer.android.com/studio/write/java8-support-table) in order to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Jun 09 '22 at 13:05
  • 2
    Your format is trying to be RFC 1123 format but fails since the valid days of the week according to that format are Sun, Mon, Tue, Wed, Thu, Fri and Sat; not Thr. The best thing you can do is to go back to the source of your string and have them correct their mistake. The string should be `Thu, 09 Jun 2022 19:20:00 GMT`. See for example [this answer](https://stackoverflow.com/a/54929530/5772882). – Ole V.V. Jun 09 '22 at 13:43
  • 2
    My comments were meant to be helpful already. If you can’t have the bug fixed, use a `DateTimeFormatterBuilder` and its `appendText(TemporalField field, Map textLookup)` to construct a formatter that accepts the incorrect texts you are receiving. – Ole V.V. Jun 09 '22 at 13:49

3 Answers3

3

In case you decide not to use outdated libraries, you may want to take a look at java.time. There's a kotlinx.datetime currently under development, but it is based on java.time anyway.

You could parse your String like this:

import java.time.format.DateTimeFormatter
import java.time.ZonedDateTime
import java.util.Locale

fun main() {
    // your example
    val input = "Thu, 09 Jun 2022 19:20:00 GMT"
    // define a formatter that parses Strings like yours
    val dtf = DateTimeFormatter.ofPattern("EEE, dd MMM uuuu HH:mm:ss O",
                                          Locale.ENGLISH)
    // parse the String and get a ZonedDateTime
    val zdt = ZonedDateTime.parse(input, dtf)
    // print the result using the ISO format
    println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME))
    // or print it using the formatter that parsed your example
    println(zdt.format(dtf))
}

This code outputs two lines:

2022-06-09T19:20:00Z
Thu, 09 Jun 2022 19:20:00 GMT

I suggest you make your fun return a java.time.ZonedDateTime


For completeness…
You can get rid of defining your own formatter in this case, there's a prebuilt one for the RFC1123 format for date plus time. The following code does the same as the one above but uses the built-in DateTimeFormatter.RFC_1123_DATE_TIME:

fun main() {
    // your example
    val input = "Thu, 09 Jun 2022 19:20:00 GMT"
    // choose a prebuilt formatter that parses RFC-formatted Strings
    val rfc1123dtf = DateTimeFormatter.RFC_1123_DATE_TIME
    // parse the String using the formatter and get a ZonedDateTime
    val zdt = ZonedDateTime.parse(input, rfc1123dtf)
    // print the result using the ISO format
    println(zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME))
    // or print it using the formatter that parsed your example
    println(zdt.format(rfc1123dtf))
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • getting crash as Caused by: java.time.format.DateTimeParseException: Text 'Thu, 09 Jun 2022 19:35:00 GMT' could not be parsed: length=29; index=29 – android dev Jun 09 '22 at 14:05
  • This code definitely works, but it's hard to tell if the code from your comment does. It seems to not parse the argument `time: String`. – deHaar Jun 09 '22 at 14:38
2

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

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47
0

checking your pattern on enter link description here , seems to me you need change it from:

val pattern = "EEE, dd MMM yyyy HH:mm:ss Z";

to:

val pattern = "EEE, dd MMM yyyy HH:mm:ss z";
alrama
  • 618
  • 11
  • 17