简体   繁体   中英

converting string date (without separator) to date format in scala

i have some issues to convert a column with Dates. my date column Looks like this "20032016" "ddMMyyyy". When i convert it, i get only null. I used following code:

df.withColumn("reporting_date",date_format(to_date(col("reporting_date"), "ddMMyyyy"), "yyyy-MM-dd")).show 

does someone can help me?

Your code example works for me:

scala> val df = Seq("20032016").toDF
df: org.apache.spark.sql.DataFrame = [value: string]

scala> df.withColumn("reporting_date",date_format(to_date(col("value"), "ddMMyyyy"), "yyyy-MM-dd")).show
+--------+--------------+
|   value|reporting_date|
+--------+--------------+
|20032016|    2016-03-20|
+--------+--------------+

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