简体   繁体   中英

cast the string column to date for the particular format

How to cast the string column to date column and maintain the same format in spark data frame?

I want to cast the string column to date by specifying the format, but the after cast date always comes in the default format which is yyyy-MM-dd.

But I want the Date type with the format which is in the string value(I want the data type as Date only not as String)

For example:

 val spark = SparkSession.builder().master("local").appName("appName").getOrCreate()
 import spark.implicits._
 //here the format is MMddyyyy(For Col2 which is of String type here)
 val df = List(("1","01132019"),("2","01142019")).toDF("Col1","Col2")

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

 //Here i need the Col3 in Date type and with the format MMddyyyy But it is converting into yyyy-MM-dd
  val df1 = df.withColumn("Col3",to_date($"Col2","MMddyyyy"))

 //I tried this but this will give me Col3 in String data type which i need in Date
  val df1 = df.withColumn("Col3",date_format(to_date($"Col2","MMddyyyy"),"MMddyyyy"))

That's not possible, Spark accepts date type yyyy-MM-dd format only.

If you need to have MMddyyyy this format date field then store as String type(if we cast to date type results null) , While processing change the format and cast as date type.

Ex:

df.withColumn("Col3",$"col2".cast("date")) //casting col2 as date datatype Results null
  .withColumn("col4",to_date($"col2","MMddyyyy").cast("date")) //changing format and casting as date type
  .show(false)

Result:

+----+--------+----+----------+
|Col1|    Col2|Col3|      col4|
+----+--------+----+----------+
|   1|01132019|null|2019-01-13|
|   2|01142019|null|2019-01-14|
+----+--------+----+----------+

Schema:

df.withColumn("Col3",$"col2".cast("date"))
  .withColumn("col4",to_date($"col2","MMddyyyy").cast("date"))
  .printSchema

Result:

root
 |-- Col1: string (nullable = true)
 |-- Col2: string (nullable = true)
 |-- Col3: date (nullable = true)
 |-- col4: date (nullable = true)

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