简体   繁体   中英

In spark Data frame how to convert Date column of type string to Date column of type Date using scala

I have tried this way in spark

df.withColumn(s"$new_col_name" unix_timestamp(date_col))

I guess I have all the required libraries.

I actually figured out three ways of doing it:

.withColumn("new_col_name", (col("date_col_oftypeString").cast("date")))
.withColumn("new_col_name",lit($"date_col_oftypeString").cast(DateType))
.withColumn("new_col_name", to_date(unix_timestamp($"date_col_oftypeString", "yyyy-MM-dd").cast("timestamp")))

Don't forget to import:

import org.apache.spark.sql.functions._

import org.apache.spark.sql.types.{DateType}

import java.sql.Timestamp

import java.util._

import spark.implicits._

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM