Search code examples
scalaapache-spark

How to convert timestamp string to timestamp format?


I am trying convert a timestamp string to timestamp. I am using below method but it returns 2024-03-18T00:00 but i want in a format of this yyyy-MM-dd HH:mm:ss

import java.time._
import java.time.format.DateTimeFormatter
import java.time.LocalDateTime
val datasettime="2024-03-18 00:00:00"
val datetime_format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
println(LocalDateTime.parse(datasettime,datetime_format)) //2024-03-18T00:00
println(datetime_format.parse(datasettime)); //returns 2024-03-18T00:00

Solution

  • You can do it in Spark using date_format function.

    val dt = spark
    .sql(s"SELECT date_format('${datasettime}', 'yyyy-MM-dd HH:mm:ss') as dt").as[String].collect.head
    
    println(dt)
    2024-03-18 00:00:00