How to convert this "Wed, 10 Jun 2020 10:16:24 GMT" to this "2020-05-10T12:30:45" datetimestr format

Viewed 44

I'm trying this but im getting exception at parse

{
  val format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")
  val format_input = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss Z")
    format.format(
      format_input.parse(
        "Wed, 10 Jun 2020 10:16:24 GMT"
      )
    )
}
1 Answers

I'd highly recommend using the newer java.time.LocalDateTime utility in place of the decrepit SimpleDate library.

Because your input format is one of the predefined DateTimeFormatter formats, and your desired output format is the same as the LocalDateTime default format, the solution becomes quite simple.

import java.time.format.DateTimeFormatter._
import java.time.LocalDateTime

LocalDateTime.parse("Wed, 10 Jun 2020 10:16:24 GMT"
                   ,RFC_1123_DATE_TIME).toString

//res0: String = 2020-06-10T10:16:24
Related