我在 Spark 数据框中有一列字符串数据类型(日期采用 yyyy-MM-dd 模式)
我想以 MM/dd/yyyy 模式显示列值
我的数据是
val df = sc.parallelize(Array(
("steak", "1990-01-01", "2000-01-01", 150),
("steak", "2000-01-02", "2001-01-13", 180),
("fish", "1990-01-01", "2001-01-01", 100)
)).toDF("name", "startDate", "endDate", "price")
df.show()
+-----+----------+----------+-----+
| name| startDate| endDate|price|
+-----+----------+----------+-----+
|steak|1990-01-01|2000-01-01| 150|
|steak|2000-01-02|2001-01-13| 180|
| fish|1990-01-01|2001-01-01| 100|
+-----+----------+----------+-----+
root
|-- name: string (nullable = true)
|-- startDate: string (nullable = true)
|-- endDate: string (nullable = true)
|-- price: integer (nullable = false)
我想以 MM/dd/yyyy 模式显示结束日期。我所能做的就是将列从 String 转换为 DateType
val df2 = df.select($"endDate".cast(DateType).alias("endDate"))
df2.show()
+----------+
| endDate|
+----------+
|2000-01-01|
|2001-01-13|
|2001-01-01|
+----------+
df2.printSchema()
root
|-- endDate: date (nullable = true)
我想以 MM/dd/yyyy 模式显示结束日期。我找到的唯一参考是this https://stackoverflow.com/questions/36948012/how-to-change-the-column-type-from-string-to-date-in-dataframes这并不能解决问题