How to convert custom datetime format to timestamp?

后端 未结 3 610
生来不讨喜
生来不讨喜 2020-12-11 23:11

Any Idea why I am getting the result below?

scala> val b = to_timestamp($\"DATETIME\", \"ddMMMYYYY:HH:mm:ss\")
b: org.apache.spark.sql.Column = to_timesta         


        
3条回答
  •  萌比男神i
    2020-12-11 23:46

    Try this UDF:

    val changeDtFmt = udf{(cFormat: String,
                             rFormat: String,
                             date: String) => {
      val formatterOld = new SimpleDateFormat(cFormat)
      val formatterNew = new SimpleDateFormat(rFormat)
      formatterNew.format(formatterOld.parse(date))
    }}
    
    sourceRawData.
      withColumn("ts", 
        changeDtFmt(lit("ddMMMyyyy:HH:mm:ss"), lit("yyyy-MM-dd HH:mm:ss"), $"DATETIME")).
      show(6,false)
    

提交回复
热议问题