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

apache spark-如何将时间戳记日期列设置为首选格式dd / MM / yyyy?

(apache spark - How to make timestamp date column into preferred format dd/MM/yyyy?)

发布于 2020-12-02 17:23:21

我有一个列YDate的形式yyyy-MM-dd HH:mm:ss(时间戳类型),但想将其转换为dd/MM/yyyy

我试过了

df = df.withColumn('YDate',F.to_date(F.col('YDate'),'dd/MM/yyyy'))

但是得到yyyy-MM-dd

我该如何有效地做到这一点。

Questioner
Mazil_tov998
Viewed
0
mck 2020-12-03 02:48:34

使用date_format来代替:

df = df.withColumn('YDate',F.date_format(F.col('YDate'),'dd/MM/yyyy'))

to_date转换给定的格式,而date_format转换指定的格式。