spark 中时间和日期 操作,怎么找到前几天后几天


1. timestamp 增加减少一个 time delta

 


  df.withColumn("added_hours",col("input_timestamp") + expr("INTERVAL 2 HOURS")) .withColumn("added_minutes",col("input_timestamp") + expr("INTERVAL 2 minutes")) .withColumn("added_seconds",col("input_timestamp") + expr("INTERVAL 2 seconds")) .show(false)

 

+-----------------------+-----------------------+-----------------------+-----------------------+ |input_timestamp |added_hours |added_minutes |added_seconds | +-----------------------+-----------------------+-----------------------+-----------------------+ |2019-07-01 12:01:19.101|2019-07-01 14:01:19.101|2019-07-01 12:06:19.101|2019-07-01 12:02:14.101| |2019-06-24 12:01:19.222|2019-06-24 14:01:19.222|2019-06-24 12:06:19.222|2019-06-24 12:02:14.222| |2019-11-16 16:44:55.406|2019-11-16 18:44:55.406|2019-11-16 16:49:55.406|2019-11-16 16:45:50.406| |2019-11-16 16:50:59.406|2019-11-16 18:50:59.406|2019-11-16 16:55:59.406|2019-11-16 16:51:54.406| +-----------------------+-----------------------+-----------------------+-----------------------+


2. Date增加减少一个 delta

 

Seq(("06-03-2009"),("07-24-2009")).toDF("date").select( col("Date"), add_months(to_date(col("Date"),"MM-dd-yyyy"),3).as("add_months"), add_months(to_date(col("Date"),"MM-dd-yyyy"),-3).as("add_months2"), date_add(to_date(col("Date"),"MM-dd-yyyy"),3).as("date_add"), date_add(to_date(col("Date"),"MM-dd-yyyy"),-3).as("date_add2"), date_sub(to_date(col("Date"),"MM-dd-yyyy"),3).as("date_sub") ).show()
+----------+----------+-----------+----------+----------+----------+ | Date|add_months|add_months2| date_add| date_add2| date_sub| +----------+----------+-----------+----------+----------+----------+ |06-03-2009|2009-09-03| 2009-03-03|2009-06-06|2009-05-31|2009-05-31| |07-24-2009|2009-10-24| 2009-04-24|2009-07-27|2009-07-21|2009-07-21| +----------+----------+-----------+----------+----------+----------+

ref:

  1. https://sparkbyexamples.com/spark/spark-add-hours-minutes-and-seconds-to-timestamp/ 时间操作
  2. https://sparkbyexamples.com/spark/spark-functions-adding-days-months-year/ 日期操作

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM