How to convert custom datetime format to timestamp?

后端 未结 3 619
生来不讨喜
生来不讨喜 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条回答
  •  长情又很酷
    2020-12-11 23:31

    Use y (year) not Y (week year):

    spark.sql("SELECT to_timestamp('04JUN2018:10:11:12', 'ddMMMyyyy:HH:mm:ss')").show
    // +--------------------------------------------------------+
    // |to_timestamp('04JUN2018:10:11:12', 'ddMMMyyyy:HH:mm:ss')|
    // +--------------------------------------------------------+
    // |                                     2018-06-04 10:11:12|
    // +--------------------------------------------------------+
    

    Another example:

    scala> sql("select to_timestamp('12/08/2020 1:24:21 AM', 'MM/dd/yyyy H:mm:ss a')").show
    +-------------------------------------------------------------+
    |to_timestamp('12/08/2020 1:24:21 AM', 'MM/dd/yyyy H:mm:ss a')|
    +-------------------------------------------------------------+
    |                                          2020-12-08 01:24:21|
    +-------------------------------------------------------------+
    

提交回复
热议问题