是否有任何pyspark函数要添加下个月,如DATE_ADD(date,month(int type)) [英] is there any pyspark function for add next month like DATE_ADD(date, month(int type))
问题描述
我是spark的新手,是否有任何内置函数将显示当前日期的下个月日期,例如今天是27-12-2016,那么该函数将返回27-01-2017.我已经使用date_add()但没有添加月份的功能.我已经尝试过date_add(date,31),但是如果月份有30天该怎么办?
I am new in spark , is there any built in function which will show next month date from current date like today is 27-12-2016 then the function will return 27-01-2017. i have used date_add() but no function for adding month. I have tried date_add(date, 31)but what if the month has 30 days .
spark.sql("select date_add(current_date(),31)") .show()
有人可以帮助我解决这个问题吗?我需要为此编写自定义函数吗?因为我仍然没有找到任何内置代码 提前致谢 卡良(Kalyan)
could anyone help me about this problem. do i need to write custom function for that ? cause i didn't find any built in code still Thanks in advance Kalyan
推荐答案
这不是pyspark
特定的.您可以使用add_months
.自 Spark 1.5 起可用.例如:
This is not pyspark
specific. You can use add_months
. It's available since Spark 1.5. e.g :
spark.sql("select current_date(), add_months(current_date(),1)").show()
# +--------------+-----------------------------+
# |current_date()|add_months(current_date(), 1)|
# +--------------+-----------------------------+
# | 2016-12-27| 2017-01-27|
# +--------------+-----------------------------+
您还可以使用负整数删除月份:
You can also use negative integers to remove months :
spark.sql("select current_date(), add_months(current_date(),-1) as last_month").show()
# +--------------+----------+
# |current_date()|last_month|
# +--------------+----------+
# | 2016-12-27|2016-11-27|
# +--------------+----------+
这篇关于是否有任何pyspark函数要添加下个月,如DATE_ADD(date,month(int type))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!