简体   繁体   English

Pyspark DD-MMM-YYYY(字符串格式)到时间戳

[英]Pyspark DD-MMM-YYYY (string format) to timestamp

Hello I am new to the Pyspark, I have a string Variable that contain date DD-MMM-YYYY format and i want to convert that into a time stamp ?您好,我是 Pyspark 的新手,我有一个包含日期 DD-MMM-YYYY 格式的字符串变量,我想将其转换为时间戳?

24-MAY-2019 - String format to timestamp 24-MAY-2019 - 时间戳的字符串格式

Try with to_timestamp(preferred) (or) from_unixtime and unix_timestamp functions:尝试使用to_timestamp(preferred) (or) from_unixtime and unix_timestamp函数:

Example:

from pyspark.sql.functions import *
from pyspark.sql.types import *
df.selectExpr("to_timestamp(dt,'dd-MMM-yyyy') as tt").show()
+-------------------+
|                 tt|
+-------------------+
|2019-05-24 00:00:00|
+-------------------+

df1.withColumn("ts",to_timestamp(col("dt"),'dd-MMM-yyyy')).show()
+-----------+-------------------+
|         dt|                 ts|
+-----------+-------------------+
|24-MAY-2019|2019-05-24 00:00:00|
+-----------+-------------------+

#using from_unixtime and unix_timestamp
df1.withColumn("ts",from_unixtime(unix_timestamp(col("dt"),'dd-MMM-yyyy'),'yyyy-MM-dd HH:mm:ss.SSS').cast("timestamp")).show(10,False)
+-----------+-----------------------+
|dt         |ts                     |
+-----------+-----------------------+
|24-MAY-2019|2019-05-24 00:00:00.000|
+-----------+-----------------------+

#using unix_timestamp and casting to timestamp
df1.withColumn("ts",unix_timestamp(col("dt"),'dd-MMM-yyyy').cast("timestamp")).show()
#+-----------+-------------------+
#|         dt|                 ts|
#+-----------+-------------------+
#|24-MAY-2019|2019-05-24 00:00:00|
#+-----------+-------------------+

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

相关问题 如何创建日期格式为“ dd-MMM-yyyy”的配置单元表? - How to create hive table with date format 'dd-MMM-yyyy'? 从字符串中提取日期格式为“DD MMM YYYY” - Extract date from string in format “DD MMM YYYY” c# 如何从包含格式的字符串中仅提取格式(例如 string = "Printed on {0:dd MMM yyyy}" 并且我想要 dd MMM yyyy - c# How to extract just the format from a string containing a format (e.g. string = "Printed on {0:dd MMM yyyy}" and I want dd MMM yyyy 如何将字符串中的多个时间戳转换为yyyy / mm / dd hh:mm:ss格式? - How to convert multiple timestamp in string to yyyy/mm/dd hh:mm:ss format? 使用 jQuery/JS 将字符串类型的时间戳转换为“DD/MM/YYYY hh:mm A”格式 - Converting a timestamp of type string into “DD/MM/YYYY hh:mm A” format using jQuery/JS PHP : 将 YYYY-MMM-DD 转换为 YYYY-MM-DD - PHP : Convert YYYY-MMM-DD to YYYY-MM-DD 检查字符串的格式是否为dd.mm.yyyy - Check if string is in format dd.mm.yyyy 如何检查日期是否格式正确,即yyyy-mmm-dd? - How to check if date is in correct format i.e. yyyy-mmm-dd? 日期的正则表达式,格式为DD / MMM / YYYY:HH24:mm:ss - Regex for date in format DD/MMM/YYYY:HH24:mm:ss 将DateTime转换为字符串格式yyyy-MM-dd时出错 - Error converting DateTime to string format yyyy-MM-dd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM