![](/img/trans.png)
[英]How to compare string dates in scala without converting it into date type
[英]converting string date (without separator) to date format in scala
我在轉換帶有日期的列時遇到了一些問題。 我的日期列看起來像這樣“20032016”“ddMMyyyy”。 當我轉換它時,我只得到空值。 我使用了以下代碼:
df.withColumn("reporting_date",date_format(to_date(col("reporting_date"), "ddMMyyyy"), "yyyy-MM-dd")).show
有人可以幫助我嗎?
您的代碼示例對我有用:
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|
+--------+--------------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.