问题描述
我有一个带有以下格式的日期的sparksql日期框架:"26MAR2015".在以下问题中,他们将 to_date 函数与java simpledataformat一起使用:,以将字符串转换为日期.我在以下问题中找不到有关此格式的更多信息:将pyspark字符串转换为日期格式.
I have a sparksql dateframe with dates in the following format: "26MAR2015".In following question they use the to_date function with java simpledataformat: Convert date from String to Date format in Dataframes to convert the strings in date. I could not find more information about this format in following question: Convert pyspark string to date format.
我找不到适合我的情况的格式:
I do not find the correct format for my case:
spark.sql("""
SELECT TO_DATE(CAST(UNIX_TIMESTAMP('15MAR2015', '??????') AS TIMESTAMP)) AS newdate"""
).show()
推荐答案
您应该使用 ddMMMyyyy
作为格式字符串.
You should use ddMMMyyyy
as the format string.
例如,您可以这样做:
spark.sql("""
SELECT CAST(FROM_UNIXTIME(UNIX_TIMESTAMP('15MAR2015', 'ddMMMyyyy')) AS date) AS newdate"""
).show()
#+----------+
#| newdate|
#+----------+
#|2015-03-15|
#+----------+
您可以找到有关Java SimpleDateFormat
的更多信息此处.
You can find more information about the Java SimpleDateFormat
here.
这篇关于在SparkSQL中将日期从字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!