Warm tip: This article is reproduced from serverfault.com, please click

Converting timestamp format in dataframe

发布于 2020-12-02 13:27:59

I have read a csv file and made a dataframe where timestamp column is in format "11/12/2020 3:01".

How do I convert this into "yyyy-mm-dd hh:mm:ss.ssssss" format for the data of that particular timestamp column?

Questioner
DigiLearner
Viewed
0
mck 2021-01-10 16:47:26
import org.apache.spark.sql.functions._

df.withColumn("timestamp_col",
    date_format(
        unix_timestamp($"timestamp_col", "dd/MM/yyyy h:mm").cast("timestamp"),  
        "yyyy-MM-dd hh:mm:ss.SSSSSS"
    )
)