简体   繁体   中英

Convert String to Timestamp in Spark/Hive

I need to convert string to Timestamp.

The problem is that the input is coming from a csv file and contains date-time values such as:

Mar 3 2022 8:30AM

Apr 27 2022 7:37AM

If I use the following conversion:

to_timestamp(to_timestamp(trim(DateColumn), 'MMM dd yyyy h:mma'), 'yyyy-MM-dd HH:mm:ss')

It converts the date Apr 27 2022 7:37AM correctly, but throws error while converting Mar 3 2022 8:30AM because of the extra space between the Month and Date values and that the date 3 is not 03.

Is there a way to convert these 2 strings formats into Datetime?

建议先统一用一个空格替换多个single space ,再转换为时间戳。

val df1 = df.withColumn("ts", to_timestamp(regexp_replace(trim(col("ts")), "\\s+", " "), "MMM d y h:mma"))

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