简体   繁体   English

将 DataFrame 字符串列转换为时间戳

[英]Convert DataFrame String colum to Timestamp

I am trying the following code to convert a string date column to a timestamp column:我正在尝试以下代码将字符串日期列转换为时间戳列:

val df = Seq(
    ("19-APR-2019 10:11:10"),
    ("19-MAR-2019 10:11:10"),
    ("19-FEB-2019 10:11:10")
  ).toDF("date")
  .withColumn("new_date", to_utc_timestamp(to_date('date, "dd-MMM-yyyy hh:mm:ss"), "UTC"))

  df.show

It almost works but it lost hours它几乎可以工作,但它失去了几个小时

+--------------------+-------------------+
|                date|           new_date|
+--------------------+-------------------+
|19-APR-2019 10:11:10|2019-04-19 00:00:00|
|19-MAR-2019 10:11:10|2019-03-19 00:00:00|
|19-FEB-2019 10:11:10|2019-02-19 00:00:00|
+--------------------+-------------------+

Do you have any idea or any other solution?您有任何想法或任何其他解决方案吗?

as SMaz mentioned in comment, followings lines do the tick:正如 SMaz 在评论中提到的那样,以下几行打勾:

import org.apache.sql.functions.to_timestamp

df.withColumn("new_date", to_timestamp('date, "dd-MMM-yyyy hh:mm:ss"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM